Reloading UITableView shows error?

人走茶凉 提交于 2020-01-20 07:52:19

问题


I am in a strange situation . when i try to reload the tableview using reloadData() it shows the following error . . .

fatal error: unexpectedly found nil while unwrapping an Optional value

Here is the web service method that retrieves output

func didRecieveOutput(results:NSArray) { 
if results.count != 0 
{ 
userOrders = results as! NSMutableArray 
dispatch_async(dispatch_get_main_queue(), { () -> Void in        
self.orderList.reloadData() })
 } 
}

Edit : I had checked my connection as well as delegate & datasource . It works fine with static data . But problem came when I called reloadData(). I had the same problem with static data as well as dynamic (data from server).


回答1:


There could be n number of reasons for this error. Some of the common causes are:

  1. Your @IBOutlet for your UITableView is not properly connected.
  2. Missing Delegate/Datasource could also be a reason.
  3. Your model that feeds data to table views is being modified just before reloadData() call.
  4. You are not properly checking for nil before using some objects.
  5. Post getting server response, you are creating a new instance instead of using the one that was already loaded.
  6. Another reason could be if your view structure is like this: UITableViewController ---> UIView ---> UITableView, then 'tableView' goes nil and you need to call out [[self.view.subviews objectAtIndex:0] reloadData];. Reference: Apple Discussion Forum.

You can try above cases but for us to pin point the error you would need to share your table view rendering code and flow.




回答2:


Solution found !!!! :)

Thanks to Abhinav for giving me the thread even though his suggestion failed.

var array : NSArray = self.view.subviews
array.objectAtIndex(0).reloadData()

I got the same error for this also. So I tried this and worked

var array : NSArray = self.view.subviews
array.objectAtIndex(2).reloadData()

Here in this array my tableview is at 2nd index. So my suggestion is to check the array to identify the tableview object first and use the index.



来源:https://stackoverflow.com/questions/32694221/reloading-uitableview-shows-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!