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
, firstName "Clayton Farr" dir ve hücre içeriği "Clayton Farr" dir
, sadece adı "Hally Wernstorm" doldurur.
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.
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.) –
Oh, sağ olduğunuz için teşekkürler! Bu hatayı daha önce yaptım, ama her zaman unutuyorum. – sallyp