tableView中的reloadData使性能降低 [英] reloadData in tableView makes performance slow

查看:33
本文介绍了tableView中的reloadData使性能降低的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理表视图,并且遇到一个问题,例如我的表正在减速以显示数据.我写的是

 -(void)loadView {getDataForTableView();}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分{//返回该节中的行数.返回items.count;}-(无效)getDataForTableView {//此方法将完成一些工作,并返回项目数[self.tableView reloadData]} 

我的表格可以重新加载,然后在表格视图中显示项目的内容.但是,这将需要一些时间.我们如何加快进度,以便表视图更快地显示数据?

更新:

这是我做的:

 <代码>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {静态NSString * CellIdentifier = @"shoppingListCell";MyCustomCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];MyCustomCell.totalLabel.text = numberAsString;MyCustomCell.comment.text = currentItem.comment;返回MyCustomCell;} 

解决方案

如果您的getDataForTableView()方法正在执行服务器请求,则在主线程上,所有UI都将冻结,直到请求返回.如果是同步请求.如果它是异步的,但是您的应用程序仍在等待重新加载数据,则不会冻结UI,而是界面会在等待某些东西.

我将在另一个线程上或使用NSInvocationOperation进行请求,并在请求返回时让它发布通知/调用回调.只要确保通知处理程序通过performSelectorOnMainThread与UIKit对象进行任何/全部对话即可.Apple规定所有UIKit代码必须在主线程上执行,尽管将消息发送到其他线程上的UIKit对象不一定会导致应用程序崩溃或引起编译器警告,但这将导致非常不可预知的行为(并可能导致崩溃).

从用户界面的角度来看,如果您必须仅在显示表格视图时执行请求(出于某些合理的原因),请确保显示正在加载"活动指示器(请参见UIActivityIndi​​catorView)或类似的东西.

但是,理想情况下,您甚至应该在显示表之前加载数据并将其缓存在后台.并定期检查其是否陈旧,并在需要时从服务器获取最新数据.考虑一下Foursquare或Path如何在其应用程序中使用场所数据来实现这一点.

I'm working on the table view and I'm facing a problem such that my table is slowing down to display a data. What I have coded is

-(void) loadView {
     getDataForTableView();
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return items.count;
}
-(void) getDataForTableView {
    // This method will do some stuff and will return the count of items
    [self.tableView reloadData]
}

My table can reload and then display the content of items in table view. However, it is going to take a while to do this. How can we speed up the progress so that the table view will display data faster?

UPDATE:

What I did is this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"shoppingListCell";
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    MyCustomCell.totalLabel.text = numberAsString;
    MyCustomCell.comment.text = currentItem.comment;
    return MyCustomCell;
} 

解决方案

If your getDataForTableView() method is doing a server request, on the main thread, all UI will freeze until the request returns. If it is a synchronous request. If it is asynchronous, but your app is still waiting on it for reloading data, it won't be a UI freeze, but the interface will just be waiting for something.

I would do the request on a different thread or using NSInvocationOperation, and have it post a notification / call a callback when the request comes back. Just make sure that the notification handler does any/all talking to UIKit objects via performSelectorOnMainThread. Apple dictates all UIKit code has to execute on the main thread, and although sending messages to UIKit objects on other threads won't necessarily crash your app or cause a compiler warning, it will lead to very unpredictable behavior (and also maybe crashes).

From a user interface perspective, if you must only do the request at the time the table view is shown (for some valid reason), make sure you show a "loading" activity indicator (See UIActivityIndicatorView) or some such thing.

Ideally, though, you should load the data and cache it in the background, before the table is even shown. And check it periodically for staleness and get fresher data from the server when needed. Think about how foursquare or Path do this with venue data in their apps.

这篇关于tableView中的reloadData使性能降低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆