Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift - UITableViewCell with left detail and right detail and subtitle?

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
}
like image 333
Tom Avatar asked Mar 25 '18 08:03

Tom


2 Answers

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
}
like image 174
Lenin Avatar answered Oct 27 '22 02:10

Lenin


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.

enter image description here

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.

enter image description here

like image 2
Martin Muldoon Avatar answered Oct 27 '22 03:10

Martin Muldoon