iOS:CellForRowAtIndexPath单元格正在混淆

Jor*_*hns 5 objective-c uitableview ios

首先说我见过这些问题:

iOS:UITableView在滚动速度过快时会混合数据

(自定义)UITableViewCell在滚动后混音

在UITableView中滚动后混合的项目

第一个和最后一个看起来与我的问题非常相关,但是我相当确定每个部分都有逻辑来确定单元格中应该出现什么(数据),但它们仍然混淆了.

以下是相关代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Note: the if (cell == nil) thing is no longer required in iOS 6
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}


if (closestRenter != nil)
{
    NSLog(@"CLOSEST RENTER!");
    [self setupCellsWithClosestRenterCell:cell atIndexPath:indexPath];
}
else
{
    NSLog(@"NO CLOSEST RENTER");
    [self setupCellsWithNoClosestRenterCell:cell atIndexPath:indexPath];
}

if (indexPath.section == 0)
{
    for (UIView *view in cell.contentView.subviews)
    {
        NSLog(@"WHAT THE HECK");
        [view removeFromSuperview];
    }
}

return cell;
Run Code Online (Sandbox Code Playgroud)

}

相关信息:

1)ClosestRenter不是......它存在.所以else子句永远不应该执行......就是这样.

2)在代码中:

[self setupCellsWithClosestRenterCell:cell atIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

有一个简单的:

if (indexPath.section == 0)
{
    cell.textLabel.text = @"PLACE HOLDER";
}
else
{
    // Populate the cell with data. (creates a view (with controller etc) and loads it into the cell)
}
Run Code Online (Sandbox Code Playgroud)

3)任何时候都有2个部分.

问题是第0部分(第一部分)应该只有那个占位符字符串.第1部分应该包含我的自定义子视图(在单元格中,它会这样做).

第0节最初只有占位符字符串,但是一旦我向下滚动(并且该部分不再可见)并向上滚动(快速),它有时会在第1部分中有一个看似随机的单元格...那到底是什么?怎么样?我不愿意把细胞重用归咎于细胞再利用,但在这一点上我真的很傻,我不知道它是什么.

这里令人不安的部分是第0节中的单元格(那里只有一行)没有子视图.但是当我快速向上和向下滚动时,它会得到一个(显然是第1部分),然后我得到了"WHAT THE HECK"日志消息......

值得一提的是,使用for循环(具有heck消息的那个)确实解决了问题(因为它删除了不需要的子视图)但必须有更好的方法.现在感觉不对劲.

有任何想法吗?

(请随意将其标记为副本,但我相当确定此处还有其他内容).

谢谢.

Jor*_*hns 4

因此,经过一点挫折和仔细分析后,我发现了细胞混合的原因。

我对单元重用(特别是标识符)的假设是问题所在。

以前我是这样做的:

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
Run Code Online (Sandbox Code Playgroud)

这固然很好,但有一个关键问题。所有单元在技术上都是相同的......在它们全部分配(不是零)之后,系统无法确定要重用哪个单元,无论它是哪个部分。

这意味着任何单元都可以从队列中抓取,无论里面有什么,然后卡在任何地方(尽管我进行了检查以确保第 1 部分的东西进入第 1 部分,而第 0 部分的东西(假承租人)留在那儿) 。

解决方案:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Note: the if (cell == nil) thing is no longer required in iOS 6
static NSString *CellIdentifier1 = @"Cell";
static NSString *CellIdentifier2 = @"Cell2";

UITableViewCell *cell;

if (indexPath.section == 0)
{
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1];
    }
    else
    {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    }

}
else
{
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1];
    }
    else
    {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
    }
}


if (closestRenter != nil)
{
    NSLog(@"CLOSEST RENTER!");
    [self setupCellsWithClosestRenterCell:cell atIndexPath:indexPath];
}
else
{
    NSLog(@"NO CLOSEST RENTER");
    [self setupCellsWithNoClosestRenterCell:cell atIndexPath:indexPath];
}

return cell;
Run Code Online (Sandbox Code Playgroud)

}

如您所见,第 0 部分将获得自己的单元格标识符。与第 1 节一样。结果是,当单元格要出队时,它将检查 indexPath 当前位于哪个部分并获取正确的单元格。

呃,这是一个令人沮丧的问题,但现在一切都有意义了:)