ded
ded

Reputation: 1350

How to add label text to DetailDisclosureButton?

I'm working in a iOS Swift 2.0 application. I can't figure out for the life of me on how to set the text on the right side of a UITableViewCell just before the disclosure indicator chevron (besides creating a custom cell.accessoryView).

Here is a screenshot of the "Settings app" doing exactly what I'm trying to achieve.

cell accessory view screenshot

Upvotes: 13

Views: 5145

Answers (3)

Abhinav Mathur
Abhinav Mathur

Reputation: 8196

For anyone trying to do it programmatically without using the storyboard at all, the easiest way would be:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let viewModel = viewModels[indexPath.row]
        let cell = UITableViewCell(style: .value1, reuseIdentifier: "cell")
        var config = cell.defaultContentConfiguration()

        config.text = viewModel.title
        config.secondaryText = viewModel.secondaryText
        cell.contentConfiguration = config
        cell.accessoryType = .disclosureIndicator
        return cell
    }

Upvotes: 1

Dmitry Klochkov
Dmitry Klochkov

Reputation: 2645

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "CellId") ?? UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "CellId")
    cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
    cell.textLabel?.text = "Main text"
    cell.detailTextLabel?.text = "Detail Text"

    return cell
}

Upvotes: 11

Dave Batton
Dave Batton

Reputation: 8835

In Interface Builder, when setting up your cell, select the Right Detail style:

Right Detail

Then assign the value to the detailTextLabel property:

cell.detailTextLabel.text = "Kilroy Was Here"

Upvotes: 16

Related Questions