开发者

Cell background incorrectly repeating in UITableViewCell when scrolling

I have a full list that populates a UITableView.开发者_JS百科 This I want to background the one of the cells with a different color and it works initially, but for some reason when I start scrolling the table up and down, it starts drawing more cells with the green background.

Please note that there is always one detailCell.detailTimeLabel.text that's equal to currentTime.

The code I have is:

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

... SOME MORE CODE HERE ...

        if ([detailCell.detailTimeLabel.text isEqualToString:currentTime]) {  
            UIView* backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
            backgroundView.backgroundColor = [UIColor greenColor];
            detailCell.backgroundView = backgroundView;
            for (UIView* view in detailCell.contentView.subviews) 
            {
                view.backgroundColor = [UIColor clearColor];
            } 
        }   
    }

Which can be the problem?


Your trying to store data in a tableviewcell. You can't do this because the cells are constantly reused. The background you see repeated occurs because its the same cell being displayed over and over again with different text.

When you dequeue the cell, you need to reset it to blank and wipe out all the previous data. Then you should set the background color only if the data you are putting into the cell has the current time.

As a general rule, you should never refer to data in the cells. If you need to know what is in a particular cell, look at the data at the indexpath within the datamodel itself.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜