2016-03-31 20 views
0

UITableViewCells'i arama ve filtrelemeden sonra dolduran basit bir sorun üzerinde çalışıyorum. İşte Filtrelemeden sonra uitableviewcell'i yerleştirme, hücre içeriği her zaman aynı

hücre doldurma için bazı kodlar: Ben kodumu bir sürü o yüzden hata ayıklamak ve burada harf "t" aradığında sonuçları olduğunu olabilir ayrılan
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell; 
    .... etc 
     if ([self.searchController isActive] && (![self.searchController.searchBar.text isEqualToString:@""])) { 
      NSArray *results = [self.searchResults mutableCopy]; 
      if (results.count == 0) { 
       // do nothing 
      } else { 
       for (NSInteger i = 0; i < results.count; i ++) { 
        NSString *fullName = [results objectAtIndex:i]; 
        ((SSContactTableViewCell *)cell).fullNameLabel.text = fullName; 
       } 
      } 
     } else { .. etc 

:

Sonuç dizimdeki 6 öğeye sahip olduğumu görebiliyorum, bunların hepsi 't' harfini içeriyor. geri kalanı da iyi görünüyor uygulamama Ancak enter image description here enter image description here

, firstName "Clayton Farr" dir ve hücre içeriği "Clayton Farr" dir enter image description here

, sadece adı "Hally Wernstorm" doldurur. enter image description here

Bunun tabii ki gerçekleşmesini istemiyorum, bu yüzden kodda bütün "Hally Wernstorm" ı geri alıyorum diye merak ediyordum?

yazdım bazı kod:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell; 
    if (self.contactsButton.selected) { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"ContactCell" forIndexPath:indexPath]; 
     if ([self.searchController isActive] && (![self.searchController.searchBar.text isEqualToString:@""])) { 
      NSArray *results = [self.searchResults mutableCopy]; 
      if (results.count == 0) { 
       // do nothing 
      } else { 
       for (NSInteger i = 0; i < results.count; i ++) { 
        NSString *fullName = [results objectAtIndex:i]; 
        ((SSContactTableViewCell *)cell).fullNameLabel.text = fullName; 
       } 
      } 
     } else { 
      NSArray *sectionArray = [self.contacts filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [self.indexTitles objectAtIndex:indexPath.section]]]; 
      NSString *fullName = [sectionArray objectAtIndex:indexPath.row]; 
      ((SSContactTableViewCell *)cell).fullNameLabel.text = fullName; 
      ((SSContactTableViewCell *)cell).specialtyLabel.text = [self.specialities objectAtIndex:indexPath.row]; 
      [((SSContactTableViewCell *)cell).avatarButton setTitle:[NSString initialsOfStrings:[fullName componentsSeparatedByString:@" "]]]; 
     } 
    } 
    else if (self.patientsButton.selected) { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"PatientCell" forIndexPath:indexPath]; 
     NSArray *sectionArray = [self.patients filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [self.indexTitles objectAtIndex:indexPath.section]]]; 
     ((SSPatientTableViewCell *)cell).fullNameLabel.text = [sectionArray objectAtIndex:indexPath.row]; 
    } 
    else if (self.favouritesButton.selected) { 
//  cell = [tableView dequeueReusableCellWithIdentifier:@"ContactCell" forIndexPath:indexPath]; 
    } 
    return cell; 
} 

#pragma mark - UISearchControllerDelegate 

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController { 
    NSString *searchString = searchController.searchBar.text; 
    if (searchString == nil) { 
     self.searchResults = [self.contacts mutableCopy]; 
    } else { 
     self.searchResults = [[NSMutableArray alloc] init]; 

     for (NSString *name in self.contacts) { 
      if ([name.lowercaseString containsString:searchString.lowercaseString]) { 
       [self.searchResults addObject:name]; 
      } 
     } 
    } 
    [self.tableView reloadData]; 
} 
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope { 
    [self updateSearchResultsForSearchController:self.searchController]; 
} 

#pragma mark - UITableViewDataSource 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    if (self.searchController.active) { 
     if (self.searchResults.count == 0 && (![self.searchController.searchBar.text isEqualToString:@""])) { 
      return 0; 
     } 
    } 
    return 26; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (self.contactsButton.selected) { 
     NSArray *sectionArray = [self.contacts filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [self.indexTitles objectAtIndex:section]]]; 
     return [sectionArray count]; 
    } else if (self.patientsButton.selected) { 
     NSArray *sectionArray = [self.patients filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [self.indexTitles objectAtIndex:section]]]; 
     return [sectionArray count]; 
    } else if ([self.searchController isActive] && (![self.searchController.searchBar.text isEqualToString:@""])) { 
     return [self.searchResults count]; 
    } 
    return 6; 
} 

Teşekkür yardımcı olabilecek herkese.

+0

olursa olsun oluştururken hangi hücre, sonuçlarda son şey FULLNAME set kullanın. (Aslında, tüm isimleri, birbiri ardına yerleştirdiyseniz, ama sonuncusuyla bitirdiniz.) –

+0

Oh, sağ olduğunuz için teşekkürler! Bu hatayı daha önce yaptım, ama her zaman unutuyorum. – sallyp

cevap

3

Bu, tableView aramayı yönetmek için uygun bir yöntem değildir.

Filtrelenmiş bir dizi oluşturmanız ve her değiştiğinde tablonuzu yeniden yüklemeniz gerekir.

+0

Normalde bir for döngüsü ile yapmıyorum; Daha önce yaptığım buydu: ((SSContactTableViewCell *) cell) .fullNameLabel.text = [self.searchResults objectAtIndex: indexPath.row]; ama çözmem gereken bölümlerle ilgili problemim var. Yorum için teşekkürler! – sallyp

1

Döngüyü kullanmak doğru bir yol değildir.

For döngüsünü kullanırken her zaman aynı adı alıyorsunuz.

Lütfen bu geçmesi ve bu search bar in objective-c demo

+0

Teşekkür ederim. Normalde bunu başka bir şekilde yaparım: \t ((SSContactTableViewCell *) cell) .fullNameLabel.text = [self.searchResults objectAtIndex: indexPath.row]; ama çözmem gereken bölümlerle ilgili problemim var, bu yüzden döngü yolunu denedim. – sallyp

İlgili konular