1

I'm using Xcode7.2.1 iOS9.2 SDK.

set the custom cell style with Right detail

  1. Register cell nib:

Code:

self.TbuserList.registerNib(UINib.init(nibName: "UserCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "idUserList")
  1. set the cell detailTextLabel text:

Code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("idUserList",forIndexPath: indexPath ) as! UserCell
    //print(self.users)
    cell.textLabel?.text = self.users[indexPath.row]["nickname"] as? String
    cell.detailTextLabel?.text = (users[indexPath.row]["isConnected"] as! Bool) ? "Online" : "Offline"
    cell.detailTextLabel?.textColor = (users[indexPath.row]["isConnected"] as! Bool) ? UIColor.greenColor() : UIColor.redColor()
    return cell
}

enter image description here

Note:When I change the cell style from "Right detail" to "subtitle" or "Left detail", it's ok.

mclaughj
  • 12,645
  • 4
  • 31
  • 37
Levi Han
  • 21
  • 5

2 Answers2

1

Since you are using a custom cell created from a xib file, I would advise avoiding trying to use the default cell elements (textLabel and detailTextLabel), and instead just add the views you need to create the cell you require. You can add additional views to the standard cells, but it can be a little more complicated to make sure your views work with the existing standard cell views. If a standard cell type would suit your needs, you can register the UITableViewCell class with the tableView instead of a custom xib file. Have a look at the section Customizing Cells in the Apple docs

mclaughj
  • 12,645
  • 4
  • 31
  • 37
Dennis W.
  • 646
  • 6
  • 7
0

I don't use Storyboard so I'm not sure if this is any help to you, but what I so in my custom cell is this:

class CustomTableViewCell: UITableViewCell {

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        //MARK: Setting ".value1" in super.init(style: .value1, ...) is the key to do this
        super.init(style: .value1, reuseIdentifier: reuseIdentifier)

        textLabel?.text = "Main Label"

        detailTextLabel?.text = "Detail Label"
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

So long as you add a new Swift file and register the same names for your custom cell it should work.

Linus
  • 423
  • 5
  • 12