目标c - 在两个tableview单元格IOS之间给出空间

时间:2017-05-26 13:20:43

标签: ios objective-c

我找到了一种在两个UItableviewcells之间添加空间的方法就像在facebook中找到一个解决方案,请检查下面的代码

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.contentView.backgroundColor = [UIColor whiteColor];
UIView  *whiteRoundedView = [[UIView alloc]initWithFrame:CGRectMake(8, 8, self.view.frame.size.width-18, cell.contentView.frame.size.height - 18)];
CGFloat colors[]={1.0,1.0,1.0,1.0};//cell color white
whiteRoundedView.layer.backgroundColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), colors);
whiteRoundedView.layer.masksToBounds = false;
whiteRoundedView.layer.cornerRadius = 5.0;
whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, 1);
whiteRoundedView.layer.shadowOpacity = 0.3;
whiteRoundedView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
[cell.contentView addSubview:whiteRoundedView];
[cell.contentView sendSubviewToBack:whiteRoundedView];}

这段代码给了我想要的输出,但问题是 当我滚动tablview时,它一次又一次地调用并使我的应用程序变慢 当我向下滚动时,阴影也在不断增加 任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:3)

TableViewCells被重用 - 如果你有很多行,它们会被反复使用。每次调用willDisplayCell:时,您都要添加另一个 whiteRoundedView。因此,在您滚动一下并且单元格已被重复使用10次后,您已添加了10个其他子视图。滚动一段时间,现在您已经添加了 几十个 的子视图。并且你为每一行做了那个。

你可以:

a)检查添加的子视图是否存在,只在第一次添加时才添加,或者......

b)您可以创建自定义UITableViewCell并根据需要对其进行格式化(更好的选择)。

答案 1 :(得分:1)

只需在单元格中添加一个视图,与单元格相比,高度减少20像素,并将y位置设置为10

将单元格背景颜色设置为清除,并将视图颜色设置为单元格背景颜色