How to Set Uitableviewcellstylesubtitle and Dequeuereusablecell in Swift

How to Set UITableViewCellStyleSubtitle and dequeueReusableCell in Swift?

Keep in mind that UITableView is defined as an optional in the function, which means your initial cell declaration needs to check for the optional in the property. Also, the returned queued cell is also optional, so ensure you make an optional cast to UITableViewCell. Afterwards, we can force unwrap because we know we have a cell.

var cell:UITableViewCell? = 
tableView?.dequeueReusableCellWithIdentifier(reuseIdentifier) as? UITableViewCell
if (cell == nil)
{
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
reuseIdentifier: reuseIdentifier)
}
// At this point, we definitely have a cell -- either dequeued or newly created,
// so let's force unwrap the optional into a UITableViewCell
cell!.detailTextLabel.text = "some text"

return cell

How to set tableview style to have subtitle programmatically while having dequeueReusableCell?

Swift 5

//Declare the variable cell Identifier

let reuseCellIdentifier = “cellIdentifier”;

//Implementation of cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var cell = tableView.dequeueReusableCell(withIdentifier: reuseCellIdentifier)
if (!(cell != nil)) {

cell = UITableViewCell(style: .subtitle, reuseIdentifier: reuseCellIdentifier)
}

cell?.textLabel?.text = //Title text
cell?.detailTextLabel?.text = //Subtitle text

return cell!
}

How to dequeue subtitle UITableViewCell in code properly?

I think your problem may be that you have registered a cell in interface builder (or in viewDidLoad) with the same name as 'cellIdentifier'.
You shouldn't register any cell if you want to use the subtitle type cell. By registering a cell, it will try to create that cell first (which will not be a subtitle type of cell).

UITableViewCell style and dequeueReusableCellWithIdentifier

If called first, table view registerClass will cause dequeueReusableCellWithIdentifier to return non-nil cell if the cell reuse identifier matches.

I believe registerClass is generally used for cells that will be a custom cell derived from UITableViewCell. Your custom cell can overrite initWithStyle and set the style there.

It's not always necessary to create a custom cell.

If you want to set the cell style then don't call registerClass.

How to use dequeueReusableCellWithIdentifier in Swift?

You can implicitly unwrap the parameters to the method and also cast the result of dequeueReusableCellWithIdentifier to give the following succinct code:

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell

//configure your cell

return cell
}

UITableView cell creation on Swift

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)
}

UITableViewCell set style programmatically

What you want is UITableViewCellStyleValue1, but you can't set an existing cell's style: you have to set it when you're creating it. Like this:

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"YourIdentifier"];


Related Topics



Leave a reply



Submit