重用细胞...... :(

时间:2011-05-15 09:11:57

标签: objective-c ios uitableview ios4 reusability

我有一个包含9个部分和56行的表格。

我想为每个单元格添加文本标签。我创建了一个NSArray menuList,其中包含56 NSDictionaries和一个包含每个部分(sectionsArray)行数的数组。

这是我的代码,但根本无法正常运行:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //Use existing cell (reusable)
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

    //If no existing cell, create a new one
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];

        //Define cell accessory type
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        //Create a subView for the cell
        CGRect subViewFrame = cell.contentView.frame;
        subViewFrame.origin.x += kInset;
        subViewFrame.size.width = kInset + kSelectLabelWidth;

        UILabel *selectedLabel = [[UILabel alloc] initWithFrame:subViewFrame];

        //SubView design
        selectedLabel.textColor = [UIColor blackColor];
        selectedLabel.highlightedTextColor = [UIColor whiteColor];
        selectedLabel.backgroundColor = [UIColor clearColor];

        [cell.contentView addSubview:selectedLabel];

        int indRow = 0;
        for (int i =0; i < indexPath.section; i++) {
            indRow += [[sectionsArray objectAtIndex:i] intValue];
        }
        indRow += indexPath.row;

        NSDictionary *cellText = [menuList objectAtIndex:indRow];

        selectedLabel.text = [cellText objectForKey:@"selection"];
        [selectedLabel release];

    }
    return cell;        
}

此代码有什么问题?

iOSSimulator中,我看到当我滚动时,单元格的文本会发生变化,标签的顺序不正确。

1 个答案:

答案 0 :(得分:6)

填写您的单元格的所有代码都在:

if (cell == nil) {

语句,所以你最终创建了少量的单元格并填充它们(当cell == nil时),然后只返回出列的单元格。

这应该是:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //Use existing cell (reusable)
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

    //If no existing cell, create a new one
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];

        //Define cell accessory type
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        //Create a subView for the cell
        CGRect subViewFrame = cell.contentView.frame;
        subViewFrame.origin.x += kInset;
        subViewFrame.size.width = kInset + kSelectLabelWidth;

        UILabel *selectedLabel = [[UILabel alloc] initWithFrame:subViewFrame];

        //SubView design
        selectedLabel.textColor = [UIColor blackColor];
        selectedLabel.highlightedTextColor = [UIColor whiteColor];
        selectedLabel.backgroundColor = [UIColor clearColor];

        [cell.contentView addSubview:selectedLabel];
    }
    // At this point cell whether it is a reused cell or a new one
    // cell points to the object we need to fill in.

    int indRow = 0;
    for (int i =0; i < indexPath.section; i++) {
        indRow += [[sectionsArray objectAtIndex:i] intValue];
    }
    indRow += indexPath.row;

    NSDictionary *cellText = [menuList objectAtIndex:indRow];

    selectedLabel.text = [cellText objectForKey:@"selection"];
    [selectedLabel release];

    return cell;       
}

这就是代码正在做的事情:

Try to get a reusable cell

If no reusable cell is available
{
    create a new cell
}

Fill in the values for the cell

因此,当一个单元格滚动屏幕时,它会被放入重用队列中。当一个单元格即将进入屏幕时,将调用cellForRowAtIndexPath。分配新单元比重用现有单元要慢,因此首先尝试从重用队列中获取单元,但如果没有可用,则创建一个新单元。然后你填写你的价值观。

相关问题