0

我做了一个UITableViewCell,我在那个表中有 20 行,屏幕上一次有 5 行。我在选择行的 didSelect 委托方法中设置了辅助视图复选标记。我担心的是假设选择了第一行并检查了它的附件类型,现在如果我滚动表格,我们会看到第六行也被选中。我知道细胞正在重用自己,而不是再次创造自己。

4

2 回答 2

1

该模型应该能够处理检查哪个单元格,哪个没有。为了简化问题,您可以保留一个数组,该数组将NSIndexPath检查应检查的 s。如果当时只能检查一个,那么一个类型的 ivar 就NSIndexPath绰绰有余了。


- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[aTableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark)
    {
            // Ok this one is selected, so we will remove it from the Reference Array.

    }
    else
    {
        // Ok this one doesn't has a checkMark
        // First add the checkmark
        [[aTableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];

        // Add the NSIndexPath to the Array of references
    }
}
于 2012-06-12T06:47:31.053 回答
-1

在委托方法中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellId = [NSString stringWithFormat:@"cell%d",indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId ];
    if (cell==nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                       reuseIdentifier:cellId ] autorelease];
    }
}

将不同的单元格 ID 设置为不同的单元格行。

于 2012-06-12T06:48:23.910 回答