0

I have a UITableViewCell subclass that has 3 labels and sizes and positions the labels based on the text length. When selected all rows perform the same function. I'd like some rows to also have a detail view associated with them. So I want to add a UIButton to a few rows, but I'm not sure how to do it.

Should I create a UIButton member of the UITVCell subclass and in layoutSubviews if the button is not nil I can make room for it?

What would cellForRowAtIndexPath look like? Do I need two reuse identifiers ? One for button and one for no button? Or just create the button and the cells that have buttons will automatically detect the button in layoutSubviews and make room for it?

I can't find any examples online of someone doing this without using XIB..

EDIT - So I added the detail disclosure button as suggested below, I can detect that my cell has an accessoryType, but the accessoryView width and height are garbage.. I need a width and height because right now the accessory is on Top of my row text since I'm not accounting for it in my layoutSubviews code..

// CustomTableViewCell.m:
- (void)layoutSubviews {
    // I need a width / height here so I can accomodate the accessory button.
    if ( self.accessoryType != UITableViewCellAccessoryNone ) {
        NSLog(@"accessory! %f %f",self.accessoryView.frame.size.width, self.accessoryView.frame.size.height);
    }
}
meshy
  • 8,470
  • 9
  • 51
  • 73
  • It looks like as a UITableViewCell subclass I have an accessoryView.. Perhaps if I check to see if this is not nil and make room for it in layoutSubviews than I can even hopefully make use of tableView:accessoryButtonTappedForRowWithIndexPath: –  May 13 '11 at 01:03

1 Answers1

2

You don't need to add a UIButton to rows that have a detail view -- just allow them to have a disclosure button (note: button, not indicator!). A disclosure button, when tapped, typically shows a detail view for that cell.

To enable the disclosure button for a certain cell, do the following in the cellForRowAtIndexPath method:

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

More details here:

What's the difference between an Detail Disclosure Button and an Disclosure Indicator?

Community
  • 1
  • 1
occulus
  • 16,959
  • 6
  • 53
  • 76
  • Thank you this just saved me a lot of confusion. –  May 13 '11 at 01:32
  • If you're happy with my answer, please consider accepting it by clicking on the tick to the left of it. – occulus May 13 '11 at 17:13
  • We're still not done yet since the TVCell subclass has to have a way to account for the space taken by the disclosure button... –  May 13 '11 at 19:04