1

我想为我的表格视图单元格添加自定义背景和选定的背景图像。目前似乎当单元格被重用时,背景图像被搞砸了,顶部单元格将使用底部单元格图像,等等。

在这种情况下,我是否错误地重复使用了单元格?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UIImageView *linkAvailableImageView = nil;
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
    UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];

    UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, tableView.bounds.size.width-20, 44)];
    UIImageView *selectedBackgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, tableView.bounds.size.width-20, 44)];


    // Asset
    Asset *asset = nil;
    asset = (Asset *)[items objectAtIndex:indexPath.row];

    int count = [items count];

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

        if (indexPath.row == 0 && count > 1) {
            backgroundImage.frame = CGRectMake(10, 0, tableView.bounds.size.width-20, 45);
            backgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundTop.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
            selectedBackgroundImage.frame = CGRectMake(10, -1, tableView.bounds.size.width-20, 45);
            selectedBackgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSelectedTop.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
        } else if (indexPath.row == count-1 && count > 1) {
            backgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundBottom.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
            selectedBackgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSelectedBottom.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
        } else if (indexPath.row == 0 && count == 1) {
            backgroundImage.frame = CGRectMake(10, -1, tableView.bounds.size.width-20, 45);
            backgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSingle.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
            selectedBackgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSelectedSingle.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
        } else {
            backgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundMiddle.png"] stretchableImageWithLeftCapWidth:1 topCapHeight:10];
            selectedBackgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSelectedMiddle.png"] stretchableImageWithLeftCapWidth:1 topCapHeight:10];
        }//end

        backgroundImage.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [backgroundView addSubview:backgroundImage];
        [backgroundImage release];

        selectedBackgroundImage.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [selectedBackgroundView addSubview:selectedBackgroundImage];
        [selectedBackgroundImage release];

        cell.backgroundView = backgroundView;
        [backgroundView release];

        cell.selectedBackgroundView = selectedBackgroundView;
        [selectedBackgroundView release];

        linkAvailableImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.bounds.size.width-39, 9, 24, 24)] autorelease];
        linkAvailableImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        linkAvailableImageView.image = [UIImage imageNamed:@"MDACLinkArrow.png"];
        linkAvailableImageView.tag = 3;
        [cell.contentView addSubview:linkAvailableImageView];

    } else {
        linkAvailableImageView = (UIImageView *)[cell.contentView viewWithTag:3];
    }

    // Get asset
    cell.textLabel.opaque = NO;
    cell.textLabel.text = asset.name;
    cell.textLabel.font = [UIFont boldSystemFontOfSize:17];
    cell.textLabel.backgroundColor = [UIColor colorWithWhite:94./255. alpha:1];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.6];
    cell.textLabel.shadowOffset = CGSizeMake(0, -1);

    // Set the kind of disclosure indicator
    if ([asset.children intValue] > 0) {
        //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }//end

    // Lazy Load the image
    if (!asset.appIcon) {

        // Download icon
        [self startIconDownload:asset forIndexPath:indexPath];

        // if a download is deferred or in progress, return a placeholder image
        cell.imageView.image = [UIImage imageNamed:@"default-icon.png"]; 

    } else {
        cell.imageView.image = asset.appIcon;
    }//end

    return cell;

}//end
4

2 回答 2

0

抱歉,但该代码有很多问题:您正在泄漏UIViewandUIImageView对象,并且单元格的整个重用是错误的,因此是您的问题。

您应该只在零件中设置一个新单元格(带有视图)if (cell == nil),并且不要忘记release/autorelease您的视图。然后,在该块之外,您相应地配置您的单元格(设置其内容)。

我强烈建议查看一些 Apple 的示例项目!

于 2011-06-27T23:05:45.397 回答
0

这里的问题是,无论表格视图中的位置如何,您都使用相同的单元格标识符。

因此,您最初根据 indexPath.row 和计数创建单元格,但您将这些单元格与 @"Cell" 的标识符相关联。因此,当您向下滚动时,dequeueReusableCellWithIdentifier将返回为列表开头配置的单元格(indexPath.row == 0 && count > 1)并将其用于列表末尾。

您需要确保单元格标识符反映了 cell==nill if 块开头的代码,以便您只重用已为您正在创建的表中的位置配置的单元格。

正如Eiko指出的那样,您还泄漏了 UIView 和 UIImageView 对象。您可以将它们粘贴在 if 块中,显式释放它们或让它们自动释放。

于 2011-06-27T23:13:22.143 回答