【问题标题】:Rounded Cell with space padding in uitableview在uitableview中带有空格填充的圆形单元格
【发布时间】: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


【解决方案1】:

是的,当您为单元格标识符“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;

}

注意:您可以将自己的唯一 id 放入可重复使用的标识符中。可重用标识符中的行ID仅供参考

【讨论】:

  • 感谢您的回答。当我滚动到列表末尾时,我尝试了你的代码并且滚动效果很好。但是当我向后滚动时,单元格会相互重复,并且性能会降低。
  • 你是动态创建单元格还是使用xib?
  • 不,我正在使用 uitableviewcontroller 并将单元格子类化到另一个类中。
  • 使用“动态标识符”会破坏可重用单元格的目的,因此总是分配一个新单元格,从而导致内存使用量增加。我无法相信如此糟糕的黑客攻击已被接受为答案。
【解决方案2】:

如果您使用相同大小的单元格,最有效的方法是使用带有帽插入的背景图像作为背景视图,在 tablesViewCells 中设置角层并不是一个好主意。

在这篇文章中,他们评论了一些使用图层自定义视图的方法article,在 cmets comments 中,Apple 的 UIKit 工程师说内存和 CPU 方面的最佳方式是使用可调整大小的图像背景技术。

另请注意,如果您不对所有单元格使用相同的标识符,您可能会最终浪费大量内存用于长表格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多