开发者

Cell background incorrectly repeating in UITableViewCell when scrolling

开发者 https://www.devze.com 2022-12-25 06:15 出处:网络
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 t

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

精彩评论

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