【发布时间】:2016-06-03 12:48:11
【问题描述】:
我有UITableViewCell,其中包含UIView(我们称之为CPView),它是在调用cellForRowAtIndexPath 时创建的。 CPView 只是一个纯色视图,每个单元格的宽度都不同(这就是为什么需要在 cellForRowAtIndexPath 中创建)。
问题是
1)CPView 的颜色在每次加载单元格时都会变深(可能是由于每次该单元格创建相同的视图时会产生重叠效果)。
2) 单元格重叠/继承其他单元格的CPView(我们可以看到这是因为两个CPView 的浅色和深色)。
如果单元格已经存在或再次创建此CPView,我如何防止它重新创建?
编辑
- (void)configureCell:(CreditDebitCell *)cell atIndexPath:(NSIndexPath *)indexPath {
//other code
UIView * CPView;
if (CPView){
CPView =nil;
}
else
{
CPView = [[UIView alloc] initWithFrame:CGRectMake(cell.bounds.origin.x, cell.bounds.origin.y, cell.frame.size.width*[self.percentArray[indexPath.row] floatValue] ,cell.frame.size.height )];
[CPView setClipsToBounds:YES];
[CPView setBackgroundColor:[UIColor colorWithRed:107/255.0 green:15/255.0 blue:47/255.0 alpha:0.5]];
[cell addSubview: CPView];
}
}
【问题讨论】:
-
写出有问题的 cellforRowAtIndexPath
-
没错。如果没有看到您的 cellForRowAtIndexPath 函数,就无法提出修复建议。 (我认为您错过了对 dequeueReusableCellWithIdentifier 的调用。)
-
@MikeGledhill 我在 cellForRowAtIndexPath 中编写了 dequeueReusableCellWithIdentifier 方法。
标签: ios objective-c uitableview uiview