问题
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:
- Your
@IBOutlet
for yourUITableView
is not properly connected. - Missing Delegate/Datasource could also be a reason.
- Your model that feeds data to table views is being modified just before
reloadData()
call. - You are not properly checking for
nil
before using some objects. - Post getting server response, you are creating a new instance instead of using the one that was already loaded.
- 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