【发布时间】:2014-02-16 21:44:44
【问题描述】:
我知道这是一个古老的问题,但这次我有点扭曲。
正如标题所说,我的 UITableView 正在重复内容。在这种情况下,单元格的背景颜色会发生变化,当滚动它并返回屏幕时它不应该发生变化。
我知道这可能是由于重复使用了单元格,但我的问题是我不能只给每个单元格一个唯一的单元格标识符,因为我已经在我的 Storyboard 中构建了我的单元格,这需要在其中输入它。
那么我怎样才能解决这个问题,同时将我的单元设计保留在我的故事板中?
一点代码:
static NSString *cellIdentifier = @"Cell";
SLDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (!cell)
{
cell = [[SLDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell setBackgroundColor:[UIColor slateBackgroundGrey]]; //Set standard background colour
[cell.titleLbl setText:[self.tableTitles objectAtIndex:indexPath.row]]; //cell titles from an array (static data I made)
//This will occur for each cell, here are two examples
if ([cell.titleLbl.text isEqualToString:@"Aircraft"])
{
NSString *detailText = @"";
if (!self.flight.aircraft) detailText = @"N/A";
else detailText = self.flight.aircraft.name;
[cell.detailLbl setText:detailText];
}
if ([cell.titleLbl.text isEqualToString:@"Delete"])
{
UIView *containerView = [cell.titleLbl superview];
[containerView setBackgroundColor:[UIColor triggerRed]];
[cell.titleLbl setTextColor:[UIColor whiteColor]];
[cell.disclosureIndicator setHidden:YES];
[cell.detailLbl setText:@""];
}
在删除单元格的情况下,我更改了单元格的颜色属性。
将prepareForReuse 子类化以重置单元格数据无法停止重复数据,在每个单独的单元格中设置颜色 if 语句也是如此(如您在上面看到的)。
谢谢。
【问题讨论】:
-
设置背景颜色的代码在哪里?
-
哦,有什么不同?这听起来就像每个人在重复使用不当时都会遇到的问题。
-
我见过的每一个解决方案,几十个都通过使用唯一的单元格标识符来解决这个问题,但是当你使用故事板时,这是没有用的。就是这样,只是我正在使用故事板:)
-
如果人们仅仅因为颜色不同而使用唯一的重用标识符,那么他们就做错了。再次,发布您设置背景的相关代码,以便我们向您展示正确的方式。
-
好的,我已经添加了所有需要的代码(希望如此),交叉手指可以更清楚地说明问题。
标签: ios objective-c uitableview