If you create new UITableViewController class, you will see commented method for override:
/*
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
// Configure the cell...
return cell
}
*/
You can uncomment method and it will not work because of error
'UITableView?' does not have a member named 'dequeueReusableCellWithIdentifier'
The reason is: tableView is defined as optional type "UITableView?" and you have to unwrap tableView before call the method. For example like this:
let cell = tableView!.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
But we can make them implicitly unwrapped optionals and use tableView without !
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
return cell
}
The question is: why does the xcode define them as optionals? Does it have any reason or advatages vs implicitly unwrapped optionals? Can be we sure, that this method always gets not-nill values?
Also we will have another errors
Constant 'cell' inferred to have type 'AnyObject!', which may be unexpected
Type 'AnyObject!' cannot be implicitly downcast to 'UITableViewCell'; did you mean to use 'as' to force downcast?
We can fix it by adding as UITableViewCell to the end like this:
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
I have no idea why doesn't template look like this by default:
/*
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell //or your custom class
// Configure the cell...
return cell
}
*/