Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView cell creation on Swift

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?

like image 767
erdemgc Avatar asked Aug 20 '14 07:08

erdemgc


People also ask

What is UITableView in Swift?

A view that presents data using rows in a single column.

How do I populate UITableView?

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.

What is tableView cell?

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.

What is difference between tableView and Collectionview in Swift?

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.


2 Answers

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)
}
like image 192
Woodstock Avatar answered Sep 18 '22 10:09

Woodstock


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.

like image 41
Anorak Avatar answered Sep 18 '22 10:09

Anorak