Search code examples
uitableviewstatictableviewcelldetailtextlabel

Set detailtextlabel of static cell programmatically


I have a tableView with a few sections and I have it set for static cells instead of dynamic prototypes. The problem is that I can't set the detail text label of a static cell programmatically or at least I don't know how. Is it possible ? The only way I see of doing this is having dynamic prototypes which means I'm going to have to deal with setting up all the cell.textLabels in my dataSource and also all the sections and my segues will not work anymore. If anyone has ideas it would be great help. Thanks :)


Solution

  • Assuming that your UITableView is in a UITableViewController, here are 2 approaches that are useful:

    Custom UITableViewCell: In the view controller class, declare a property for a label as: @property (strong) IBOutlet UILabel *labelInCell;

    In the storyboard, drag a UILabel into the cell, select the controller's Connections Inspector, and connect the outlet of the property by dragging from the inspector to the UILabel object.

    You can then assign the label text programmatically, for example, in viewDidLoad: of the controller class.

    Standard datasource: Alternatively, you can implement just one method: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath in the view controller's datasource and set the detailTextLabel property there.