Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple buttons in accessory view of UITableView?

I want to add two adjacent custom buttons in the UITableView's accessory view.

I tried doing cell.accessoryView = customButton; and then cell.accessoryView = customButton2 .It is obvious that this replaces the previous button.

Thanks for any help!

like image 777
motox Avatar asked Feb 27 '14 10:02

motox


2 Answers

You can add a UIView containing the two buttons as a custom accessoryView.

UIView *buttonsView = [...];
// add buttons to buttonsView
cell.accessoryView = buttonsView;

Or you can subclass UITableViewCell and add two buttons there.

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {
        UIButton *buttonA = ....
        UIButton *buttonB = ....
        [self.contentView addSubview:buttonA];
        [self.contentView addSubview:buttonB];
    }

    return self;
}

If you haven't done a custom UITableViewCell before this article might help.

http://code.tutsplus.com/tutorials/ios-sdk-crafting-custom-uitableview-cells--mobile-15702

like image 126
3lvis Avatar answered Nov 19 '22 08:11

3lvis


You can make a custom UIView class with your buttons placed inside of it (as subviews: [self addSubview:button]). Then you can assign your custom UIView object as accessoryView of a cell.

cell.accessoryView = yourCustomView;

like image 2
damirstuhec Avatar answered Nov 19 '22 08:11

damirstuhec