我有 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/