Steve Kuo
Steve Kuo

Reputation: 63134

Control UITableViewCell's style

I want to use a UITableViewCell as-is, but have it style as Subtitle. My understanding is that the style can only be set in the initializer. The problem is that the initializer is called implicitly, and I have no way to specify the style.

In my UITableViewController's viewDidLoad() I register the cell and reuse ID

tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "defaultCell")

Then in my cellForRowAtIndexPath

let cell = tableView.dequeueReusableCellWithIdentifier("defaultCell", forIndexPath: indexPath)

cell is always non-nil. The alternative is to bypass cell reuse and always create a new instance

let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "defaultCell")

Is there a way to specific the style and get cell reuse?

Upvotes: 1

Views: 71

Answers (2)

rmaddy
rmaddy

Reputation: 318955

You don't need to bypass cell reuse to get what you want. Do it the old fashioned way.

Don't call registerClass:forCellReuseIdentifer:.

Use dequeueReusableCellWithIdentifer: instead of dequeueReusableCellWithIdentifer:forIndexPath:.

When the call to dequeueReusableCellWithIdentifer: returns nil, you create the cell explicitly with the desired style.

Upvotes: 4

sschale
sschale

Reputation: 5188

You can just change the style in the Storyboard.

enter image description here

Upvotes: 0

Related Questions