0

我正在使用 tableview 并且正在对 uitableview 进行多次检查。一切都很完美,我得到了正确的值,但是当我滚动表格视图时,它丢失了复选标记图像(使用默认复选标记没有自定义图像),但选定的值保留在数组中......

滚动表格视图会删除复选标记图像。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *app= (AppDelegate *)[[UIApplication sharedApplication]delegate];

    if([_tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){

    NSLog(@"yes");

    [placesvisitedarray removeObject:[app.nameArray objectAtIndex:indexPath.row]];


    [_tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;

    }
    else
    {
        NSLog(@"no");

        [_tableView cellForRowAtIndexPath:indexPath].accessoryType =  UITableViewCellAccessoryCheckmark;

        [placesvisitedarray addObject:[app.nameArray objectAtIndex:indexPath.row]];

    }
   // [_tableView reloadData];

}
4

1 回答 1

1

复选标记被删除,因为当您滚动时,cellForRowAtIndexPath:正在调用 tableview 并重新创建单元格。

您可以编写一个方法来检查数组中是否存在某个值:

- (BOOL)stringExistsInPlacesVisited:(NSString *)stringToMatch {
    for (NSString string in placesvisitedarray) {
        if ([string isEqualTo:stringToMatch])
            return YES;
    }
    return NO;
}

然后,cellForRowAtIndexPath:您必须检查 placesvisitedarray 并插入/删除复选标记。

if ([stringExistsInPlacesVisited:[app.nameArray objectAtIndex:indexPath.row])
    cell.accessoryType =  UITableViewCellAccessoryCheckmark;
else
   cell.accessoryType =  UITableViewCellAccessoryNone;

代码未经测试,因此可能无法正常工作,但至少它可以让您了解如何进行。

于 2013-02-10T11:40:46.563 回答