ios - UITableView 和 Autolayout 性能不佳

标签 ios objective-c uitableview autolayout

我有 20 个不同的自定义 UITableViewCells(而且越来越多),而且它们之间的差异非常大(不同的内容,不同的高度...)。

我从我的服务器获取所有数据源,因此需要一段时间才能在我的 UITableView 中提供内容。

我使用代码和 Autolayout 构建我所有的自定义单元格。

当我现在打开我的 UITableView 并开始滚动时,UITableView 在可见性方面停滞了片刻,我不确定如何解决这个问题问题。

以下是一些重要的代码片段:

- (void)viewDidLoad 
{

[super viewDidLoad];

self.tableView.estimatedRowHeight = 250.0f;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GTBlockCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//Here i save my data from the Server into my Model(GFBlock)
GFBlock *block = [self.node.blocks objectAtIndex:indexPath.row];

//I think this part, is the reason why it stucks sometimes, here i call a method which build each of my Custom Cells
cell = [[GTAppController sharedInstance]  createInterfaceForBlock:block];

[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];

return cell;
}


//Create the Interface for each Cell
- (GTBlockCell *)createInterfaceForBlock:(GFBlock *)block 
{

NSString* className = [self.interfacesDict objectForKey:NSStringFromBlockType(block.blockTypeId)];

Class c = NSClassFromString(className);


//Open the specific Custom Cell
GTBlockCell* instance = [(GTBlockCell *)[c alloc] initWithBlock:block];

return instance;
}

我还应该提到,我的一些自定义单元格的行高极其不同。我还尝试使用 estimatedRowHeight 但实际上没有任何改变。

也许你们中的一些人还有其他一些好主意?

最佳答案

首先,使用工具找出您的代码真正花费时间的地方,而不是猜测应该归咎于自动布局。

其次,您没有重复使用您的细胞。滚动性能在很大程度上取决于单元格的重用。

GTBlockCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

这一行使一个单元格出队

cell = [[GTAppController sharedInstance]  createInterfaceForBlock:block];

这条线抛弃了它并建立了一个全新的。

您需要将填充单元格与创建单元格分开。如果您有几种不同类型的单元格,则为每个单元格使用一个重用标识符,以便您将适当类型的单元格出队,然后用正确的数据填充它。在开始之前用 tableview 注册每个表格单元格子类,然后你总是会从表格中得到一个新的单元格或一个回收的单元格。

关于ios - UITableView 和 Autolayout 性能不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34086131/

相关文章:

ios - 分段控制 - 发送到实例的无法识别的选择器

objective-c - 具有不同数组设置的 UITableView 多个部分

ios - 将 Sprite Kit 与空应用程序混合

uitableview - 实现 heightForRowIndexPath : in iOS 7, 但在 iOS 8 中使用编译时宏删除它

ios - 设置 rowHeight 等于 UITableViewAutomaticDimension 不起作用

ios - 动态 Tableviewcell 中的按钮不可点击

ios - 组合:监听内部集合变化

ios - 如何覆盖/删除应用程序中保存的图像

ios - rootViewController 是否始终准备好在应用程序 :didBecomeActive is called (iOS)? 之前呈现 segue

objective-c - 测量线程等待锁所花费的时间(由 coredata 锁定)