uitableview 中填充 space 的圆形单元格
Rounded Cell with space padding in uitableview
我想在 uitableview 上的每个单元格之间创建 space 的圆形单元格。
像这样:
正如我在 Whosebug 中搜索的那样,有两种方法。
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;
}
但是当我滚动时它仍然表现不佳。
是的,当您为 "PlaceholderCell2":
的单元格标识符传递静态密钥时,它会导致性能下降
您必须传递动态标识符,例如:
NSString *reuseIdentifier = [NSString stringWithFormat:@"cell_%@",indexPath.row];
SubTableViewCell * sctvCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (sctvCell == nil) {
sctvCell= [[CellMessageDetail alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
它会做的是检查是否为特定的行 ID 创建单元格,如果没有创建单元格或使用已经创建的单元格。
为效果动态创建单元格:
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self){
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];
return self;
}
注意::您可以将自己的唯一标识放入可重用标识符中。 Reuseable identifier中的row id仅供参考
如果您使用相同大小的单元格,执行此操作的最有效方法是使用带有 cap insets 的背景图像作为背景视图,在 tableViewCells 中设置角层并不是一个好主意。
在这篇文章中,他们评论了一些使用图层自定义视图的方法article, and in the comments here comments一位 Apple 的 UIKit 工程师说,内存和 CPU 明智的最佳方法是使用可调整大小的图像背景技术。
另请注意,如果您不对所有单元格使用相同的标识符,您可能会为长表浪费大量内存。
我想在 uitableview 上的每个单元格之间创建 space 的圆形单元格。
像这样:
正如我在 Whosebug 中搜索的那样,有两种方法。
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;
}
但是当我滚动时它仍然表现不佳。
是的,当您为 "PlaceholderCell2":
的单元格标识符传递静态密钥时,它会导致性能下降您必须传递动态标识符,例如:
NSString *reuseIdentifier = [NSString stringWithFormat:@"cell_%@",indexPath.row];
SubTableViewCell * sctvCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (sctvCell == nil) {
sctvCell= [[CellMessageDetail alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
它会做的是检查是否为特定的行 ID 创建单元格,如果没有创建单元格或使用已经创建的单元格。
为效果动态创建单元格:
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self){
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];
return self;
}
注意::您可以将自己的唯一标识放入可重用标识符中。 Reuseable identifier中的row id仅供参考
如果您使用相同大小的单元格,执行此操作的最有效方法是使用带有 cap insets 的背景图像作为背景视图,在 tableViewCells 中设置角层并不是一个好主意。
在这篇文章中,他们评论了一些使用图层自定义视图的方法article, and in the comments here comments一位 Apple 的 UIKit 工程师说,内存和 CPU 明智的最佳方法是使用可调整大小的图像背景技术。
另请注意,如果您不对所有单元格使用相同的标识符,您可能会为长表浪费大量内存。