When using dequeueReusableCellWithIdentifier
, I am simply unable to get a detailTextLabel
in the UITableViewCell
.
If I allocate a new UITableViewCell
each time in cellForRowAtIndexPath
, the detailTextLabel
is shown, but obviously I'll get a memory leak this way.
Why is this happening, and how do I manage to show a detailTextLabel
with reusable cells?
Update: I don't use Storyboard and it's a basic system cell. I realise now that dequeue...
never returns nil, so where am I supposed to configure the cell as style .Subtitle
?
class SomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let bounds = UIScreen.mainScreen().bounds
tableView = UITableView(frame: bounds, style: .Plain)
tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
self.view.addSubview(self.tableView)
}
// uses dequeue
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath);
if cell == nil {
cell = UITableViewCell(style: .Subtitle, reuseIdentifier: reuseIdentifier)
}
cell.textLabel?.text = "Text"
cell.detailTextLabel?.text = "Detail NOT displayed"
return cell
}
// doesn't use dequeue...
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: reuseIdentifier) // memory leak
cell.textLabel?.text = "Text"
cell.detailTextLabel?.text = "Detail displayed"
return cell
}
}