【问题标题】:UITableView indexPath section logicUITableView indexPath 部分逻辑
【发布时间】:2011-12-02 03:04:02
【问题描述】:

这是我的 cellForRowAtIndexPath UITableView 委托方法的精简代码:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"];
if (cell == nil) {
    // No cell to reuse => create a new one
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"blahblahblah"] autorelease];
}
cell.textLabel.text = @"";
cell.detailTextLabel.text = @"";
cell.backgroundView = NULL; //problem here

// Initialize cell
//blah blah blah
//now to the good part...

if(indexPath.section == 1) {
    cell.backgroundView = deleteButton;
    cell.userInteractionEnabled = YES;
    cell.textLabel.text = nil;
    cell.detailTextLabel.text = nil;
}

else if(indexPath.section == 0) {
    NSLog(@"section: %i row: %i", indexPath.section, indexPath.row);
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"foobar";
            //more stuff
            break;

        //lots more cases

        default:
            break;
    }
}

return cell;

}

我的问题是第 1 节中的第一个单元格(第 0 节有 10 个单元格,第 1 节只有 1 个单元格)被分配了只应该分配给第一节单元格 0 的信息。因此,它没有获取 deleteButton 背景等,而是获取标签标题“foobar”。我不太确定为什么会这样,因为我的 if 语句很清楚。有什么想法吗?

编辑:将 backgroundView 设置为 NULL 会导致那些带有文本的单元格在离开视图时返回而没有任何背景。所以这不是一个可行的解决方案。此外,detailTextLabel 的文本仍设置在不应包含任何文本的单元格上。

这是它的外观,单元格 backgroundViews 设置为 nil,文本显示在不应该删除的单元格上:

解决方案,由 Alex Deem 推荐(用此代码替换旧的 dequeue 代码):

NSString* identifier;
if(indexPath.section == 0)
    identifier = @"0";
else
    identifier = @"1";
UISwitch *switchView;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
    // No cell to reuse => create a new one
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];

【问题讨论】:

    标签: ios uitableview nsindexpath


    【解决方案1】:

    您应该阅读有关细胞重用的文档。

    您应该为这两个部分中的每一个部分使用不同的重用标识符,因为它们是完全不同样式的单元格。

    【讨论】:

    • 啊,是的,做到了。用解决方案更新了问题。
    【解决方案2】:

    在这些代码行中添加一个结束 } 括号:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"];
    if (cell == nil) {
        // No cell to reuse => create a new one
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"blahblahblah"] autorelease];
    }
    ....
    ....
    ....
    if(indexPath.section == 1) {
       ....
       ....
    }
    if(indexPath.section == 0) {
       ....
       ....
    }
    

    而且我怀疑你的表格会得到更好的结果。

    现在事情的工作方式(至少据我在您的代码中所说),您创建一个单元格并将其初始化为某些内容。它永远不会重置为所请求的任何其他内容的值和数据。

    【讨论】:

    • 那没有这样做 - 现在它们部分中第 0 行的两个单元格都从第 0 部分中的单元格中获取 both 文本和从第 1 部分中的单元格中获取背景. 但是,正如我偶然发现的那样,它是为 dequeueReusableCellWithIndentifier 和 reuseIndentifier 使用不同的标识符......这是一种不好的做法吗?
    • 用您的新代码更新您的原始问题...是的,标识符在单个表视图中应该是相同的。
    • 在你做你的if (indexPath.section == 1)之前,做cell.textLabel.text = @"";cell.backgroundView = NULL;。这应该可以解决最后两个问题。哦,你真的在​​if 之前有一个else,上面没有if / then 对吗?
    • PROBLEM HERE 是什么意思,有什么问题?
    【解决方案3】:

    您的代码结构错误。

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"];
    if (cell == nil) 
    

    如果不再使用,dqueueReuseableCellWithIdentifier 将返回一个现有的先前创建的单元格。 If cell == nil 您应该创建一个新单元格并设置所有单元格共有的默认值。但是,该 indexPath 唯一的任何数据设置都应该在

    之后完成
    if(cell==nil) block.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"blahblahblah"];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1         reuseIdentifier:@"blahblahblah"] autorelease];
    }
    cell.textLabel.text=@"unique text";
    return cell;
    

    【讨论】:

    • 请参阅我在 Micheal Dautermann 的回答中发表的评论。
    猜你喜欢
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多