我正在尝试让我的应用程序中的单元格仅在标题与存储在NSUserDefaults中的NSMutableArray中的单词匹配的行上显示选中标记。我现在的问题是,它在每个应用程序上都显示一个复选标记...甚至该行不匹配的应用程序也是如此。这是我的代码和控制台日志。
- (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的行也显示选中标记。
最佳答案
您应该添加else
或将if
重写为条件代码,如下所示:
cell.accessoryType = [entry.date isEqualToString:myString]
? UITableViewCellAccessoryCheckmark
: UITableViewCellAccessoryNone;
否则,带有复选标记的单元格将在“回收”后永久保留配件。