iOS UITableView CheckMark出现在每一行

时间:2012-12-19 17:15:18

标签: ios uitableview nsmutablearray

我正在尝试让我的应用中的单元格仅在标题与存储在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: 

如您所见,返回的唯一字符串是第1天: 但是,即使是entry.date = Day 2:的行也会显示复选标记。

4 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

您应该添加else,或者将if重新写为有条件的,如下所示:

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

否则,设置了复选标记的单元格会在“回收”时永久保留附件。

答案 2 :(得分:1)

你必须写:

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

答案 3 :(得分:0)

这应该有帮助

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

}else
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}