【发布时间】:2015-02-02 11:05:58
【问题描述】:
我想在 uitableview 上创建圆形单元格,每个单元格之间有空格。
像这样:
正如我在 Stackoverflow 中搜索的那样,有两种方法。
1:在 cellForRowAtIndexPath 中使用 CornerRadius 创建单元格边界,如下所示:
[cell.contentView.layer setCornerRadius:7.0f];
[cell.contentView.layer setMasksToBounds:YES];
[cell.contentView.layer setBorderWidth:0.5f];
[cell.contentView.layer setBorderColor:[UIColor grayColor].CGColor];
然后为每个单元格创建页脚或页眉以获取每个单元格的填充。
2:创建圆形 uiview,然后像这样将它们添加到单元格中:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.contentView.backgroundColor = [UIColor clearColor];
UIView *whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(10,10,300,150)];
whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
whiteRoundedCornerView.layer.masksToBounds = NO;
whiteRoundedCornerView.layer.cornerRadius = 3.0;
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-1, 1);
whiteRoundedCornerView.layer.shadowOpacity = 0.5;
[cell.contentView addSubview:whiteRoundedCornerView];
[cell.contentView sendSubviewToBack:whiteRoundedCornerView];
}
上述方法的缺点是滚动性能不佳。
当我进一步搜索时,有人建议我应该使用单元格的子类和
然后重复使用该单元格。
这就是我创建子类的方式:
创建“SubTableViewCell”文件并将此代码添加到其中:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
self.contentView.backgroundColor = [UIColor clearColor];
UIView *whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(10,10,300,150)];
whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
whiteRoundedCornerView.layer.masksToBounds = NO;
whiteRoundedCornerView.layer.cornerRadius = 3.0;
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-1, 1);
whiteRoundedCornerView.layer.shadowOpacity = 0.5;
[self.contentView addSubview:whiteRoundedCornerView];
[self.contentView sendSubviewToBack:whiteRoundedCornerView];
}
在我的主 Uitableview 文件中:
- (SubTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *PlaceholderCellIdentifier = @"PlaceholderCell2";
SubTableViewCell * sctvCell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
sctvCell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
.....
....
return sctvCell;
}
但是当我滚动时它仍然会给出一个糟糕的性能。
【问题讨论】:
-
什么表现不好?你能详细说明一下吗?
-
在滚动大约 20 行后,滚动速度非常缓慢,并且比该程序完全冻结了更多。
-
你在牢房里有什么?我做了你在第 2 点所做的事情,我从来没有遇到过这样的问题......
-
我的单元格中有一个图像视图和几个标签。但是如果将它们从单元格中删除,滚动仍然会出现问题。就像我滚动到单元格时它每次都会重复。
标签: ios objective-c uitableview