UITableView 的 NSThread
在视图控制器的 viewDidLoad() 方法中,我调用
[NSThread detachNewThreadSelector:@selector(readFromFaceBook)
toTarget:self
withObject:nil];
readFromFaceBook 将从远程位置提取数据并将数据填充到名为“myData”的 NSMutabaleArray 中”。视图控制器包含一个从 myData 加载数据的 tableView
。
我遇到的问题是,如何延迟 tableView 委托的执行,直到所有数据都被线程加载到“myData”中..?由于委托函数尝试使用 myData 执行,程序崩溃。非常感谢任何帮助...!!!!
In the viewDidLoad()
method of view controller I am calling
[NSThread detachNewThreadSelector:@selector(readFromFaceBook)
toTarget:self
withObject:nil];
readFromFaceBook
will pull the data from remote location and populates the data in NSMutabaleArray
called "myData". The view controller contains a tableView
which loads data from myData.
The problem I have is, how to delay the execution of tableView delegates until all the data is loaded into "myData" by the thread ..?? The program is crashing as delegate functions are trying to execute using myData . Any help is greatly appreciated...!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该创建表格视图并立即显示它,这样用户就不会认为您的应用程序不起作用。也许是一个 UIProgressIndicator 或其他东西来表明你正在做某事?
正如您正确选择的那样,您的后台线程应该正在执行加载。完成后,通过类似
performSelectorOnMainThread:...
或dispatch_async()
到dispatch_get_main_queue()
回调主线程,并且调用[myTableView reloadData]
刷新 UI。为了防止表格视图过早加载,您可以采取多种方法。假设您将数据存储在数组中,因为
UITableView
显示有序数据。如果是这样,那么在线程完成之前不要填充数组。如果这不起作用,您可以使用一个BOOL
ivar 来指示是否已完成加载,然后使用BOOL
的状态来确定要执行什么操作您应该在UITableView
中显示。You should create the tableview and show it immediately, so the user doesn't think your app is non-functional. Perhaps a
UIProgressIndicator
or something to show that you're doing something?Your background thread should be doing the loading, as you've correctly chosen. When it's done, call back to the main thread via something like
performSelectorOnMainThread:...
ordispatch_async()
to thedispatch_get_main_queue()
, and invoke[myTableView reloadData]
to refresh the UI.To prevent the tableview from loading prematurely, there are several approaches you could take. Presumably you're storing your data in an array, since a
UITableView
displays ordered data. If so, then just don't populate the array until the thread finishes. If that doesn't work, you could just have aBOOL
ivar that you use to indicate whether you've finished loading, then use the state of theBOOL
to determine what you should be showing in yourUITableView
.您可以做两件事:要么在获取数据后实例化您的
tableView
,要么在收到回调后调用[tableView reloadData]
数据加载完成。Two things you can do: either instantiate your
tableView
after you get the data or call[tableView reloadData]
after you get a callback saying that your data is done loading.您的辅助线程不应该处理 UI 工作。
一旦您收到下载完成的回调并且数据位于“myData”集合中 --- 在主线程上下文中调用一个函数,假设您正在调用 UpdateTable:。
使用以下 API 在
UpdateTable 中执行此操作
Your secondary thread should not be handling UI work.
Once you get call back on download is done and data is in "myData" collection --- call a function in Main thread context let say you are calling UpdateTable:.
Use following API to do this
IN the UpdateTable