Using a UITableView
and its UITableViewCell
(s) in Swift. I have some problem showing the detailTextLabel
field of a UITableViewCell
.
I already found these two helpful posts, but could not get a fully working solution: swift detailTextLabel not showing up How to Set UITableViewCellStyleSubtitle and dequeueReusableCell in Swift?
Here is the code I am using, relevant to my question:
override func viewDidLoad() {
super.viewDidLoad()
………….
theTableView = UITableView(frame: tmpFrame)
………….
theTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
theTableView.dataSource = self
theTableView.delegate = self
self.view.addSubview(theTableView)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
cell.backgroundColor = UIColor.clearColor()
cell.textLabel?.text = "THE-TEXT-LABEL"
cell.detailTextLabel?.text = "KIJILO" // This does not show up.
return cell
}
When I try to make use of the detailTextLabel
field of UITableViewCell
, it does not show up. Searching on the net, I understand that I have to use the proper style(UITableViewCellStyle
) for the cell, but I do not see how to integrate that change into my code. The examples I've seen are based on subclassing UITableViewCell
and I suppose I do not need to subclass UITableViewCell
only to make use of the detailTextLabel
field. Please tell me if I am wrong.
I have also tried a few things from the posts mentioned above, but nothing works as I want.
You've registered the UITableViewCell
theTableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "reuseIdentifier")
This means that when you call
tableView.dequeueReusableCellWithIdentifier("reuseIdentifier",
forIndexPath: indexPath)
one will be automatically created for you using the UITableViewCellStyleDefault style.
Because you want a custom style you need to do a few things:
Remove
theTableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "reuseIdentifier")
Replace
var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier",
forIndexPath: indexPath)
with the following
var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier")
if cell == nil {
cell = UITableViewCell(style: .Value1, reuseIdentifier: "reuseIdentifier")
}
What is happening here is dequeueReusableCellWithIdentifier
will return nil if it can't dequeue a cell. You can then generate your own cell providing with the specified style.
If you are using Storyboard
:
Subtitle
I found that it's not enough to check if the cell
is nil.
I added this additional check:
var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath)
if cell == nil || cell?.detailTextLabel == nil {
cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellReuseIdentifier)
}
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