5

这是我的 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 推荐(用此代码替换旧的出队代码):

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];
4

3 回答 3

5

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

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

于 2011-12-02T03:50:10.723 回答
0

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

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) {
   ....
   ....
}

我怀疑你的桌子会有更好的结果。

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

于 2011-12-02T03:11:40.230 回答
0

您的代码结构错误。

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;
于 2011-12-02T03:15:21.527 回答