10

How to create UITableViewCell with an next style (Right Detail in xib)

enter image description here

For example can I use next:

cell.style = 'Right Detail' (roughly)

Thanks!

Juan Boero
  • 6,281
  • 1
  • 44
  • 62
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • this might be helpful - https://stackoverflow.com/questions/13174972/setting-style-of-uitableviewcell-when-using-ios-6-uitableview-dequeuereusablecel – Rakesha Shastri Jun 23 '22 at 10:31

3 Answers3

25

What you want is UITableViewCellStyleValue1, but you can't set an existing cell's style: you have to set it when you're creating it. Like this:

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"YourIdentifier"];
David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
yuji
  • 16,695
  • 4
  • 63
  • 64
  • 27
    But what happens if you're using dequeueReuseable:forIndexPath:... on a tableView? – fatuhoku Nov 15 '15 at 20:14
  • 1
    How is this done outside of the initWithStyle initializer? – wcochran Feb 12 '16 at 20:54
  • Will this work ? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:REUSEIDENTIFIER forIndexPath:indexPath]; if(!cell){ cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:REUSEIDENTIFIER]; } – Mubeen Qazi Dec 06 '18 at 09:29
  • @fatuhoku check this https://stackoverflow.com/a/72728320/7734643 – Rakesha Shastri Jun 23 '22 at 10:03
3

While the best answer for this question is correct it doesn't touch on the topic of cell reuse.

It turns out you no longer have to use tableView.register. Instead you can use this approach:

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

    // Whenever there's a reusable cell available, dequeue will return it
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier") ??
        UITableViewCell(style: .subtitle, reuseIdentifier: "cellIdentifier")
csch
  • 1,455
  • 14
  • 12
0

If you want to use the UITableViewCell and style it programmatically then Don't register it to your tableView and do this in cellForRowAt

    var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)

    if cell == nil {
       var newCell = UITableViewCell(style: .value1, reuseIdentifier: cellIdentifier)
    // custom styling on textLabel, detailTextLabel, accessoryType
    cell = newCell
   }

Note: The deque method is without IndexPath param.

Muhammad Idris
  • 2,138
  • 1
  • 13
  • 9