我正在尝试让应用程序中的单元格只在标题与存储在NSUserDefaults中的NSMutableArray中的单词匹配的行上显示复选标记。我现在的问题是,它会在每个app...even上显示一个复选标记,表示该行与任何内容都不匹配。这是我的代码和控制台日志。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
NSArray *rowsarray = [defaults objectForKey:@"checkedrows"];
NSLog(@"ORIGINAL%@", rowsarray);
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"SELF contains[cd] %@", entry.date];
NSArray *filteredArray = [rowsarray filteredArrayUsingPredicate: predicate];
NSString *myString = [filteredArray componentsJoinedByString:@""];
NSLog(@"The array %@", filteredArray);
NSLog(@"The string %@", myString);
if([entry.date isEqualToString:myString]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
UIFont *cellFont = [UIFont fontWithName:@"Papyrus" size:19];
UIFont *cellFont2 = [UIFont fontWithName:@"Papyrus" size:17];
cell.textLabel.text = entry.date;
cell.detailTextLabel.text = entry.articleTitle;
cell.detailTextLabel.textColor = [UIColor blackColor];
cell.textLabel.font = cellFont;
cell.detailTextLabel.font = cellFont2;
return cell;
}控制台日志:
2012-12-20 11:02:53.793 5MWG[3615:c07] ORIGINAL(
"Day 1: "
)
2012-12-20 11:02:53.794 5MWG[3615:c07] The array (
"Day 1: "
)
2012-12-20 11:02:53.794 5MWG[3615:c07] The string Day 1:
2012-12-20 11:06:23.851 5MWG[3615:c07] 2
2012-12-20 11:06:23.852 5MWG[3615:c07] ORIGINAL(
"Day 1: "
)
2012-12-20 11:06:23.852 5MWG[3615:c07] The array (
)
2012-12-20 11:06:23.852 5MWG[3615:c07] The string
2012-12-20 11:06:23.854 5MWG[3615:c07] 2
2012-12-20 11:06:23.854 5MWG[3615:c07] ORIGINAL(
"Day 1: "
)
2012-12-20 11:06:23.855 5MWG[3615:c07] The array (
"Day 1: "
)
2012-12-20 11:06:23.855 5MWG[3615:c07] The string Day 1: 如您所见,返回的唯一字符串是Day 1:。但是,即使是带有entry.date = Day 2:的行也会显示复选标记。
发布于 2012-12-20 01:17:46
您应该添加else,或者将if重写为条件,如下所示:
cell.accessoryType = [entry.date isEqualToString:myString]
? UITableViewCellAccessoryCheckmark
: UITableViewCellAccessoryNone;否则,设置了复选标记的单元将在被“回收”时永久保留附件。
发布于 2012-12-20 01:21:22
我没有仔细查看您的代码,但是如果您使用重用标识符出队,并且没有将cell.accessoryType设置为none,那么每个在过去进行过检查的回收单元在将来都会进行检查。
发布于 2012-12-20 01:17:39
你必须这样写:
if([entry.date isEqualToString:myString]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}https://stackoverflow.com/questions/13957630
复制相似问题