Uitableview 为什么cellForRowAtIndexPath有时会添加渐变层?
我有一个函数,看起来像这样:Uitableview 为什么cellForRowAtIndexPath有时会添加渐变层?,uitableview,Uitableview,我有一个函数,看起来像这样: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // instantiate the cell if(indexPath.row >=4 && indexPath.row <=5) { [CustomTableCell addGradient]; } } 我刚刚意识到iPho
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// instantiate the cell
if(indexPath.row >=4 && indexPath.row <=5)
{
[CustomTableCell addGradient];
}
}
我刚刚意识到iPhone/cocoa实际上是在重新使用内存中的现有单元格,以便cellForRowAtIndexpath绘制屏幕上显示的单元格。这就是为什么细胞仍然有我之前设置的梯度层。因此,我使用以下代码更新了stylecell函数,以删除存在的层:
- (void) styleCell
{
if([self.layer.sublayers count] >0)
{
if([[self.layer.sublayers objectAtIndex:0] isKindOfClass:[CAGradientLayer class]])
{
[[self.layer.sublayers objectAtIndex:0] removeFromSuperlayer];
// return;
}
}
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor blueColor].CGColor, (id)[UIColor redColor].CGColor, nil];
[self.layer insertSublayer:gradient atIndex:0];
}
@end
- (void) styleCell
{
if([self.layer.sublayers count] >0)
{
if([[self.layer.sublayers objectAtIndex:0] isKindOfClass:[CAGradientLayer class]])
{
[[self.layer.sublayers objectAtIndex:0] removeFromSuperlayer];
// return;
}
}
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor blueColor].CGColor, (id)[UIColor redColor].CGColor, nil];
[self.layer insertSublayer:gradient atIndex:0];
}
@end