how to add a right detail to a cell that already have left detail and subtitle. and how to give a textLabel to the right detail?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let monster = monsters[indexPath.row]
cell.textLabel?.text = monster.name
cell.detailTextLabel?.text = monster.subtitle
return cell
}
Try this
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let monster = monsters[indexPath.row]
cell.textLabel?.text = monster.name
cell.detailTextLabel?.text = monster.subtitle
let label = UILabel.init(frame: CGRect(x:0,y:0,width:100,height:20))
label.text = monster.name
cell.accessoryView = label
return cell
}
Click on your tableView cell. It's easier to do it in document outline.
Change the style attribute in Attribute Inspector from Basic to Custom. You will now be able to add a label. The basic template is set, so you can't alter it. I believe this was the issue you were having.
I'm sure after you place the label you know what to do, but just in case:
Place the new label at the right margin of the cell and pin it to the top, bottom, and right, using Autolayout.
Reconnect your title, subtitle IBOutlets, and create a right label IBOutlet, connect it, and you should be done.
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