开发者

UITableViewCell background repeats if cell scrolls off screen

I want to apply a special background to the first cell in my UITableView: I am applying the bg in:

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

This is how it is applied:

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

The problem is, when this cell scrolls off screen, a new cell will have indexPath.row == 1. How can I make sure only this cell has this background?

UPDATE: Here is the full method

// 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开发者_StackOverflow:@"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;        
    }
}


Show us the whole code for cellForRowAtIndexPath please. I suspect that you are simply not configuring the cells correctly. I could be wrong but are you doing this background stuff only when you actually create the cell? Cells are cached so you have to make sure that you set the background to normal for every cell that comes out of the cache.

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


Sounds like there's something wrong with the way you're reusing your cells. The problem is not that the indexPath.row equals one, but that the cell you created for the row=1 cell is being reused. Perhaps you should add an else condition:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜