37

The detail (subtitle) text does not appear. The data are available, though, because when a println() call is added, it prints Optional("data") to the console with the expected data. In the storyboard, the UITableViewController is set to the proper class, the Table View Cell Style is set to 'Subtitle', and the reuse identifier is set to 'cell'. How can I get the subtitle information to display?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    dispatch_async(dispatch_get_main_queue(), { () -> Void in

        cell.textLabel.text = self.myArray[indexPath.row]["title"] as? String
        cell.detailTextLabel?.text = self.myArray[indexPath.row]["subtitle"] as? String

        println(self.myArray[indexPath.row]["subtitle"] as? String)
        // The expected data appear in the console, but not in the iOS simulator's table view cell.

    })
    return cell
}
kurple
  • 411
  • 1
  • 5
  • 12

13 Answers13

48

Your code looks fine. Just goto the storyboard and select the cell of your tableview -> Now goto Attributes Inspector and choose the style to Subtitle.

Follow this according to the below screenshot.

Change this option

Hope it helped..

onCompletion
  • 6,500
  • 4
  • 28
  • 37
40

Same issue here (from what I've read, perhaps a bug in iOS 8?), this is how we worked around it:

  1. Delete the prototype cell from your storyboard

  2. Remove this line:

var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

  1. Replace with these lines of code:
let cellIdentifier = "Cell"
            
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
if cell == nil {
    cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: cellIdentifier)
}

Update for Swift 3.1

let cellIdentifier = "Cell"
    
var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
    cell = UITableViewCell(style: UITableViewCellStyle.value2, reuseIdentifier: cellIdentifier)
}

Update for Swift 4.2 - Simplified

let cell = UITableViewCell(style: UITableViewCell.CellStyle.value2, reuseIdentifier: "cellId")

Update for Swift 5 - Simplified

let cell = UITableViewCell(style: .value2, reuseIdentifier: "cellId")
grow4gaurav
  • 3,145
  • 1
  • 11
  • 12
wpearse
  • 2,422
  • 2
  • 29
  • 30
16

If you still want to use prototype cell from your storyboard, select the TableViewcell style as Subtitle. it will work.

Keshav
  • 2,965
  • 3
  • 25
  • 30
  • 2
    Thanks. After reworking the code, the "Subtitle" option works. – kurple Dec 01 '14 at 04:12
  • This doesn't work for me when it's a tableview inside of a regular viewcontroller, but it does in a TableViewController. Weird ios8/swift bug I guess. – CodyMace Dec 09 '14 at 23:35
  • @CodyMace:No, It will work for all tableviews wherever you are using. you might be missing something else. – Keshav Dec 10 '14 at 03:55
  • Maybe affected by this 'optimization' introduced in iOS8? http://stackoverflow.com/questions/25793074/subtitles-of-uitableviewcell-wont-update – Hugh Jeffner Mar 02 '15 at 17:38
  • works for me in TableView with Xcode: Version 6.4 (6E35b) – Tom Hert Sep 08 '15 at 05:19
  • This worked for me with Storyboard prototype cell and changing the value of the detailTextLabel. – Septronic Dec 30 '15 at 10:05
6

enter image description here

Try this it work for me (swift 5)

let cell = UITableViewCell(style: .value1, reuseIdentifier: "cellId")
cell.textLabel.text = "Déconnexion"
cell.imageView.image = UIImage(named: "imageName")

Objective c :

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellId"];
Zouhair Sassi
  • 1,403
  • 1
  • 13
  • 30
5

If you are setting the text to nil somewhere when you try to set it to a non-nil value the actual view that contains the text will be missing. This was introduced in iOS8. Try setting to an empty space @" " character instead.

See this: Subtitles of UITableViewCell won't update

Community
  • 1
  • 1
Hugh Jeffner
  • 2,936
  • 4
  • 32
  • 31
  • This was my issue but it happens when you set it to "" and nil. So you actually have to set it to " ". So basically isEmpty causes this behavior as well. – Wyeth Shamp Apr 30 '15 at 00:01
5

If doing so programmatically without cells in interface builder this code works like a charm in Swift 2.0+

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    yourTableView.delegate = self
    yourTableView.dataSource = self
    yourTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "subtitleCell")

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return yourTableViewArray.count
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    let cell: UITableViewCell = yourTableView.dequeueReusableCellWithIdentifier("subtitleCell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = "the text you want on main title"
    cell.detailTextLabel?.text = "the text you want on subtitle"

    return cell
}
Chris Klingler
  • 5,258
  • 2
  • 37
  • 43
4

For what it's worth: I had the problem of detail not appearing. That was because I had registered the tableView cell, which I should not have done as the cell prototype was defined directly in storyboard.

claude31
  • 874
  • 6
  • 8
2

In Xcode11 and Swift5 , We have to do like below. If we do it by checking the condition cell == nil and then creating UITableViewCell with cellStyle it is not working . Below solution is working for me .

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
           let cellIdentifier = "Cell"
            let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: cellIdentifier)
    cell?.textLabel?.text = "Title"
    cell?.detailTextLabel?.text = "Sub-Title"

   return cell!
    }
Abhishek B
  • 157
  • 8
  • This code, doesn't dequeue cell for tableView. You just only create Cell everytime cellForRowAt is called. – ShadeToD Jan 28 '21 at 10:29
1

Here is how it works for swift 5, to get a subtitle using detailtextlabel, using a UITableView object within a view controller, if you are missing any of these, it will not work and will probably crash.

class ViewController: UIViewController, UITableViewDelegate,  UITableViewDataSource

In viewDidLoad:

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "subtitleCell")

Delegate Function:

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

    // Fetch a cell of the appropriate type.
    let cell = UITableViewCell(style: .subtitle , reuseIdentifier: "subtitleCell")

    // Configure the cell’s contents.
    cell.textLabel!.text = "Main Cell Text"
    cell.detailTextLabel?.text = "Detail Cell Text"

    return cell
}
PDG
  • 349
  • 3
  • 10
1

xcode 11

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
    let cell = UITableViewCell(style: UITableViewCell.CellStyle.value1, reuseIdentifier: "reuseIdentifier")
    cell.detailTextLabel?.text = "Detail text"
    cell.textLabel?.text = "Label text"

    // Configure the cell...

    return cell
}
1

Some of the solutions above are not entirely correct. Since the cell should be reused, not re-created. You can change init method.

final class CustomViewCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .value1, reuseIdentifier: reuseIdentifier)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
mazy
  • 652
  • 1
  • 10
  • 18
1

Three properties will be deprecated in a future release: imageView textLabel and detailTextLabel

You can use UIListContentConfiguration to configure cell

dataSource = UITableViewDiffableDataSource(tableView: tableview, cellProvider: { tableview, indexPath, menu in
        let cell = tableview.dequeueReusableCell(withIdentifier: self.profileCellIdentifier, for: indexPath)
        var content = cell.defaultContentConfiguration()

        content.text = menu.title
        if indexPath.section == MenuSection.info.rawValue {
            content.image = UIImage(systemName: "person.circle.fill")
            content.imageProperties.tintColor = AppColor.secondary
        }
        if let subtitle = menu.subTitle {
            content.secondaryText = subtitle
        }
        cell.contentConfiguration = content
        return cell
    })
chanhbv
  • 21
  • 1
0

Swift 5 with subtitle text, here no need to register your cell in viewDidLoad:

    var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
       if cell == nil {
            cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")
       }
    
    cell?.textLabel?.text = "title"
    cell?.textLabel?.numberOfLines = 0
    cell?.detailTextLabel?.text = "Lorem ipsum"
    cell?.detailTextLabel?.numberOfLines = 0

    
    return cell ?? UITableViewCell()
msusare
  • 473
  • 2
  • 7