【问题标题】:UITableView, another cell in invisible area has a checkmark while one in visible area is touchedUITableView,不可见区域的另一个单元格有一个复选标记,而可见区域的一个单元格被触摸
【发布时间】:2014-06-03 15:10:52
【问题描述】:

这是我的代码,它显示了 14 行表格视图的问题。对于一个屏幕,可以看到 6 个单元格。

当我点按第 2 个单元格时,第 10 个单元格也有一个复选标记,点按第 3 个然后第 11 个复选标记,第 1 个然后第 9 个复选标记,点按第 1 个然后第 8 个复选标记...但是灰色突出显示的行为不是这样,只有可以突出显示一个单元格。

如果我在同一屏幕上点击一个单元格后点击一个单元格,则刚刚勾选的单元格将被清除为勾选标记,这是有道理的。但是,如果在我点击一个单元格然后向下滚动表格视图之后,我可以点击一个单元格并且它也显示复选标记,即刚刚在顶部屏幕上选中的那个仍然有复选标记。因此,如果我上下滚动,每次滚动到另一侧后点击一个单元格,我可以为每个单元格打上复选标记,所有复选标记都会显示出来。

真的很奇怪,我已经尝试了很多方法来解决它,但似乎我对UITableView失踪有一些基本的了解,请问有没有人可以解决?谢谢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID= @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }

    cell.imageView.image = [UIImage imageNamed:@"earth.png"];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath
{
    UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];    
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryNone;
}

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    您不会将复选标记的存在/不存在存储在任何类型的持久数据源中,并在cellForRowAtIndexPath 中显示/隐藏它。因此,您的表格正在重用您的单元格,它只是在找到它时将其扔掉。因此,如果它重用启用了您的复选标记附件的单元格,则会将其显示为已启用(因为您没有在 cellForRow 中专门禁用它)

    我会保留选定indexPath 对象的NSMutableArray。在didSelectRowAtIndexPath 中添加indexPath,并在didDeselectRowAtIndexPath 中删除它。然后在cellForRowAtIndexPath中检查当前的indexPath是否存在于数组中,如果存在则勾选,不存在则关闭。

    【讨论】:

    • 我只是想知道,这种知识是写在Apple的文档中的吗?我检查了 UITableView 委托方法的参考文档,它没有写太多关于如何设置附件类型的内容。我很好奇你有没有像我一样犯过错误,然后你想通了?
    • 主要是经验。但这不仅仅是配件类型。您未在cellForRow 中明确设置的任何内容都将保留在重用单元格中。因此,如果您在某处更改单元格的背景颜色,但在显示单元格时未检查/设置相同的背景颜色,则可能会被错误地重用。
    【解决方案2】:

    Stonz2's answer 是对的。 例如,您应该为表格视图数据源创建一个对象,并且它应该有一个属性来显示它是否已选中。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:     (NSIndexPath *)indexPath
    {
        ...
    
        Item *item = [self.itemArray objectAtIndex:indexPath.row];
        if (item.checkmarked) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        Item *tappedItem = [self.itemArray objectAtIndex:indexPath.row];
        tappedItem.checkmarked = !tappedItem.checkmarked;
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多