I am trying to create UITableViewCell with swift, on my delegate method cellForRowAtIndexPath,
the code is simple as in Objective-c, just trying to morph the language to swift.
I am getting error on this line
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell
if(cell == nil)
{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle,reuseIdentifier:cellIdentifier)
}
error is UITableViewCell is not convertible to "MirrorDisposition"
I have looked up examples, the code was like this
if !cell
{let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle,reuseIdentifier:cellIdentifier)
}
but it also gives an error.
What am i doing wrong?
A view that presents data using rows in a single column.
There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.
A table view tracks the height of rows separately from the cells that represent them. UITableView provides default sizes for rows, but you can override the default height by assigning a custom value to the table view's rowHeight property. Always use this property when the height of all of your rows is the same.
When would you choose to use a collection view rather than a table view? Suggested approach: Collection views are there to display grids, but also handle entirely custom layouts, whereas table views are simple linear lists with headers and footers.
As of the latest beta (beta 6) non optional types cannot be compared with nil.
Therefore you must declare your cell Var as an Optional.
Something like this will work correctly (off the top of my head - I don't have Xcode in front of me):
//declare a tableViewCell as an implicitly unwrapped optional...
var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
//you CAN check this against nil, if nil then create a cell (don't redeclare like you were doing...
if(cell == nil)
{
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,reuseIdentifier:cellIdentifier)
}
A far better option is to use the more modern method which always returns a cell (as long as you are using a storyboard, or have registered the nib or class for the cell)
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell
Since the method always returns a cell, it's not an optional, and there as no need to check for nil.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With