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 i开发者_如何学编程n 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);
}
}
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?
精彩评论