我想在uitableview上创建每个窗格之间有空格的圆角单元格。
像这样:
当我在Stackoverflow中搜索时,有两个approch。
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];
}
以上approch的缺点是滚动性能不佳。
当我进一步搜索时,有人建议我应该使用细胞的子类和
然后重复使用该单元格。
这是我如何创建我的子类:
创建" 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;
}
但是当我滚动时它仍然表现不佳。
答案 0 :(得分:3)
是的,当您传递静态密钥作为“PlaceholderCell2”的单元格标识符时,它会产生不良性能:</ p>
您必须传递动态标识符,如:
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;
}
注意::您可以将自己的唯一ID放入可重复使用的标识符中。可重复使用标识符中的行ID仅供参考
答案 1 :(得分:0)