UITableViewCell background repeats if cell scrolls off screen?

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.

Up vote 1 down vote favorite share g+ share fb share tw.

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:@"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; } } iphone objective-c cocoa-touch uitableview uitableviewcell link|improve this question edited Aug 7 '10 at 1:51 asked Aug 4 '10 at 15:50Sheehan Alam5,002360179 91% accept rate.

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; }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions