0

我试图让我的应用程序中的单元格仅在其标题与存储在 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: 的行也显示复选标记。

4

4 回答 4

3

我没有仔细查看您的代码,但是如果您使用重用标识符出队并且您没有将 cell.accessoryType 设置为 none,那么过去检查过的每个回收单元将来都会进行检查。

于 2012-12-19T17:21:21.920 回答
2

您应该添加一个else,或将其重写if为条件,如下所示:

cell.accessoryType = [entry.date isEqualToString:myString]
?   UITableViewCellAccessoryCheckmark
:   UITableViewCellAccessoryNone;

否则,带有复选标记的单元格将在“回收”后永久保留附件。

于 2012-12-19T17:17:45.820 回答
1

你必须写:

if([entry.date isEqualToString:myString]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}
于 2012-12-19T17:17:39.330 回答
0

这应该有帮助

if([entry.date isEqualToString:myString]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

}else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
于 2012-12-19T17:18:36.057 回答