目前,我有我的应用程序命中我的端点,并返回10个或更少的项目。如果返回的项目少于10,且UITableView
已显示10个项目,reloadData()
将导致错误,因为其大小与上次不同。现在,当我收到我的回复时,我所做的就是:
tableView.beginUpdates()
self.items = items //where self.items is the array that backs the UITableView, and items are the items I got back in form of JSON from the server.
tableView.reloadData()
tableView.endUpdates()
答案 0 :(得分:2)
为什么会导致错误?如果您使用的是tableView.reloadData(),则无需调用endUpdates()和beginUpdates()。只需简单地分配项目并重新加载tableView数据。
self.items = items
tableView.reloadData()
如果你在后台线程,则在主队列上调用上面的代码。
dispatch_async(dispatch_get_main_queue()) { () -> Void in
// Code runs on main queue!
}
答案 1 :(得分:2)
调用reloadData不是导致错误的原因。这是因为在更新模型后,numberOfRowsInSection或numberOfSectionsInTableView没有返回正确的值。
答案 2 :(得分:0)
在表格视图方法中,
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {}
你应该写为,返回[self.items count];
这样,你的表就会知道你的表需要显示多少项,重新加载数据不会抱怨。