如果单元格滚出屏幕,则重复UITableViewCell背景

时间:2010-08-04 15:50:50

标签: iphone objective-c cocoa-touch uitableview

我想在UITableView的第一个单元格中应用特殊背景: 我正在申请bg:

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

这就是它的应用方式:

if (indexPath.row == 1) {
            cell.contentView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"top-message-bg.png"]];
        }

问题是,当此单元格滚出屏幕时,新单元格将具有indexPath.row == 1.如何确保只有此单元格具有此背景?

更新:这是完整的方法

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

    static NSString *CellIdentifier = @"Cell";

   // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    STVCell *cell = (STVCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (indexPath.row == 0) {
        if(mainVideoCell == nil)
        {
            mainVideoCell = [[MainVideoCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MainVideoCell"];
            mainVideoCell.contentView.backgroundColor = [UIColor colorWithRed:242.0/255.0 green:242.0/255.0 blue:242.0/255.0 alpha:1.0];
        }

        // Configure the cell...
        STVideo *mySTVideo;
        mySTVideo = [items objectAtIndex:indexPath.row];

        mainVideoCell.videoTitle.text = mySTVideo.video_title;

        /*NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setFormatterBehavior:NSDateFormatterBehavior10_4];
        [formatter setDateStyle:NSDateFormatterShortStyle];
        [formatter setTimeStyle:NSDateFormatterNoStyle];
        NSString *result = [formatter stringFromDate:[NSDate date]];
        [formatter release];*/

        mainVideoCell.dateLabel.text = [Utils toShortTimeIntervalStringFromStockTwits:mySTVideo.publish_on]; //result; //[mySTVideo.publish_on substringToIndex:10];//[Utils toShortTimeIntervalStringFromStockTwits:mySTVideo.publish_on];
        mainVideoCell.videoDescription.text = mySTVideo.description;
        mainVideoCell.authorLabel.text = [NSString stringWithFormat:@"with %@", mySTVideo.author];
        [mainVideoCell.videoThumbnail loadImageFromURL:[NSURL URLWithString:mySTVideo.thumbnail_url]];
        NSLog(@"Description:%@",mySTVideo.description);

        return mainVideoCell;
    } 
    else {
        if (cell == nil) {
            //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            cell = [[[STVCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }

        // Configure the cell...
        STVideo *mySTVideo;
        mySTVideo = [items objectAtIndex:indexPath.row];

        cell.videoTitle.text = mySTVideo.video_title;

        /*NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setFormatterBehavior:NSDateFormatterBehavior10_4];
        [formatter setDateStyle:NSDateFormatterShortStyle];
        [formatter setTimeStyle:NSDateFormatterNoStyle];
        NSString *result = [formatter stringFromDate:[NSDate date]];
        [formatter release];*/

        cell.dateLabel.text = [Utils toShortTimeIntervalStringFromStockTwits:mySTVideo.publish_on];//result; //[mySTVideo.publish_on substringToIndex:10];//[Utils toShortTimeIntervalStringFromStockTwits:mySTVideo.publish_on];
        cell.videoDescription.text = mySTVideo.description;
        cell.authorLabel.text = [NSString stringWithFormat:@"with %@", mySTVideo.author];
        [cell.videoThumbnail loadImageFromURL:[NSURL URLWithString:mySTVideo.thumbnail_url]];

        if (indexPath.row == 1) {
            cell.contentView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"top-message-bg.png"]];
        }
        else {
            UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
            imageView.image = [[UIImage imageNamed:@"message-bg.png"] stretchableImageWithLeftCapWidth:1 topCapHeight:20];//[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"message-bg.png"]];
            [imageView setOpaque:YES];
            [cell setBackgroundView:imageView];
            [imageView release];
        }


        return cell;        
    }
}

2 个答案:

答案 0 :(得分:0)

请向我们展示cellForRowAtIndexPath的完整代码。我怀疑你没有正确配置单元格。我可能是错的,但只有当你真正创建单元格时,你才在做这个背景吗?单元格被缓存,因此您必须确保将每个单元格的背景设置为正常。

if (indexPath.row == 1) {
            cell.contentView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"top-message-bg.png"]];
}
else
{
     cell.contentView.backgroundColor = // whatever normal background is
}

答案 1 :(得分:0)

听起来你重复使用细胞的方式有些不对劲。问题不在于indexPath.row等于1,而是您为row = 1单元格创建的单元格正在被重用。也许你应该添加一个else条件:

if (indexPath.row == 1) {
        cell.contentView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"top-message-bg.png"]];
    }
else {
    cell.contentView.backgroundColor = [UIColor whiteColor];
}