【问题标题】:Setting cell rounded corner and border width makes tableview scrolling extremely slow设置单元格圆角和边框宽度使tableview滚动极慢
【发布时间】:2013-03-24 21:48:36
【问题描述】:

我想把我的 tableview 单元格的角落弄圆,让它们之间的间隙变大。

我在 cellForRowAtIndexPath 中这样做:

static NSString* CellIdentifier = @"normal";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if(cell == nil)
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

CALayer* layer = cell.layer;
[layer setCornerRadius:8.0f];
[layer setMasksToBounds:YES];
[layer setBorderWidth:2.0f];

通过调用 setBorderWidth,tableview 的滚动变得非常缓慢。没有它,它就像一个魅力。

那么,我怎样才能使单元格圆角和设置边框宽度更快?

【问题讨论】:

  • 这非常有效。关于如何让单元格的 imageView.layer 工作的任何想法?

标签: ios uitableview quartz-graphics


【解决方案1】:

我尝试了很多次以使其快速运行。但是当用于许多对象时,它的工作速度非常慢。

我可以推荐什么:

使用背景图片,不使用图层属性。以here 为例

或者使用 tableView 样式 - UITableViewStyleGrouped

在您的情况下,表格视图中的对象可能不是很多,因此请优化它,例如在创建单元格时设置边框:

if(cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

CALayer* layer = cell.layer;
[layer setCornerRadius:8.0f];
[layer setMasksToBounds:YES];
[layer setBorderWidth:2.0f];
}

【讨论】:

  • 我只有几个元素,大约30个,我的iphone5正在苦苦挣扎
  • 真的很多,最好创建背景图像或使用分组表视图。它会像魅力一样起作用。
  • 我的意思是列表中有30个元素,我觉得很少
  • 更多 10 通常会使设备上的 ui 变慢,模拟器一切都应该没问题。这是因为它总是要重绘单元格的图层。
【解决方案2】:

不是每次都设置图层属性,而是在像这样创建单元格时执行一次。

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

CALayer* layer = cell.layer;
[layer setCornerRadius:8.0f];
[layer setMasksToBounds:YES];
[layer setBorderWidth:2.0f];
}

【讨论】:

  • Cell 是在 storyboard 上定义的,所以没关系。
猜你喜欢
  • 2019-07-11
  • 1970-01-01
  • 2013-03-23
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 2020-01-26
相关资源
最近更新 更多