在我的应用程序中,我有一个UITableView与巨大的UITableViewCell的。每个单元都是动态的,并在运行时创建。通过说动态不只是内容,但布局也是动态的。问题是,它需要很长时间,并且在您滚动时会出现断断续续的情况。特别是当一个新的细胞进入屏幕时,至少有1秒钟冻结。如何在iOS中异步添加视图到UITableViewCell?
有什么办法可以改善这种性能吗?我检查了这个教程对异步加载细胞:
然而,本教程演示如何异步加载DATAS为静态细胞。我需要将动态数据加载到动态布局。
在我的应用程序中,我有一个UITableView与巨大的UITableViewCell的。每个单元都是动态的,并在运行时创建。通过说动态不只是内容,但布局也是动态的。问题是,它需要很长时间,并且在您滚动时会出现断断续续的情况。特别是当一个新的细胞进入屏幕时,至少有1秒钟冻结。如何在iOS中异步添加视图到UITableViewCell?
有什么办法可以改善这种性能吗?我检查了这个教程对异步加载细胞:
然而,本教程演示如何异步加载DATAS为静态细胞。我需要将动态数据加载到动态布局。
这里有一些几个技巧,我希望这将有助于你
1 >>试着在你的厦门国际银行为他们提供可重复使用的标识符重用你的tableview细胞(如果细胞是自定义)。
2 >>检查cellForTheRowAtIndexPath方法中的循环(不要执行循环)。
3 >>如果您在cellForTheRowAtIndexPath方法中下载像图像这样的数据,请确保您以异步方式下载它。
4 >>执行UI任务就像细胞在主线程像
[NSOperationQueue mainQueue]addOperationWithBlock:{
}];
我无法真正重复使用这些单元格,因为它们中的每一个都是不同的。 –
在cellForRowAtIndexPathMethod的主线程中设置标签中的图像或数据。 [NSOperationQueue mainQueue] addOperationWithBlock:{ }]; – mukul
感谢您的提示。我在cellForRow方法中有很多条件。会让他们稍微恢复一下。 –
尝试一下cellForRowAtIndexPath方法。我在单元格上添加了自定义视图,并在该自定义视图上添加了imageview。你可以添加UILabel,UIButton或其他东西。
- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSString * CellIndentifier = @"CellIndentifier";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:nil];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
}
UIView *cellVW ;
UIImageView *logoImg;
cellVW = [[UIView alloc] initWithFrame:CGRectMake(10, 2, tableView.frame.size.width-20, 216)];
cellVW.backgroundColor = [UIColor clearColor];
cellVW.layer.cornerRadius = 5;
cellVW.layer.borderWidth = 1;
cellVW.layer.borderColor = [[UIColor lightGrayColor] CGColor];
[cell addSubview:cellVW];
logoImg = [[UIImageView alloc] init];
logoImg.frame = CGRectMake(10, 5, tableView.frame.size.width-20, 140);
logoImg.backgroundColor = [UIColor clearColor];
logoImg.layer.borderColor = [[UIColor lightGrayColor] CGColor];
NSURL *imageURL = [NSURL URLWithString:@"https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg"];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:imageURL] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if(error == nil)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if([httpResponse statusCode] == 200)
{
UIImage *image = [UIImage imageWithData:data];
logoImg.image = image;
}
else
{
}
}
}];
[cellVW addSubview:logoImg];
return cell;
}
这假设在单元格中有一个UIImageView。我需要的还是创建UIImageView异步。 –
我在这里展示了两个步骤。在线程中创建元素,并在本地或从服务器设置它们的值。这是迄今为止的规则。你的主要目标应该是避免冻结UI。你的目标是什么? –
请参阅参考代码:我看过他们的动画。我做了很多次这样的工作。注意此行:dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),{() - >空隙中 让文本= self.model.textForIndexPath(indexPath) dispatch_async(dispatch_get_main_queue(),{() - 在 >空cell.textLabel?text = text }) }) –
用哪种语言,你想要你的解决方案? objc c还是swift? –
我正在使用swift。但任何解决方案都会在这一点上做到。我没有选择。 –
你有服务器上的单元格的图像吗?或只是数据? –