Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 滚动时UITableViewCell中的单元格背景不正确地重复_Ios_Objective C_Cocoa Touch_Uitableview - Fatal编程技术网

Ios 滚动时UITableViewCell中的单元格背景不正确地重复

Ios 滚动时UITableViewCell中的单元格背景不正确地重复,ios,objective-c,cocoa-touch,uitableview,Ios,Objective C,Cocoa Touch,Uitableview,我有一个填充UITableView的完整列表。我想给其中一个单元格加上不同颜色的背景,它一开始可以工作,但由于某种原因,当我开始上下滚动表格时,它开始用绿色背景绘制更多的单元格 请注意,总有一个detailCell.detailTimeLabel.text等于currentTime 我的密码是: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)inde

我有一个填充
UITableView
的完整列表。我想给其中一个单元格加上不同颜色的背景,它一开始可以工作,但由于某种原因,当我开始上下滚动表格时,它开始用绿色背景绘制更多的单元格

请注意,总有一个
detailCell.detailTimeLabel.text
等于
currentTime

我的密码是:

    - (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];
            } 
        }   
    }

哪个可能是问题?

您试图将数据存储在tableviewcell中。您不能这样做,因为这些单元不断重复使用。出现重复出现的背景是因为同一个单元格用不同的文本反复显示

当您将单元格出列时,需要将其重置为空白并清除所有以前的数据。然后,仅当放入单元格的数据具有当前时间时,才应设置背景色

一般来说,您不应该引用单元格中的数据。如果需要了解特定单元格中的内容,请查看数据模型本身中indexpath处的数据