views:

4034

answers:

3

I'd like to add a button to a table cell. The "Delete Event" in the calendar app inspired me... (a similar case is "Share Contact" in contacts)

As of now there's

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  //..yadayadayada
  cell = [tableView dequeueReusableCellWithIdentifier:@"buttonCell"];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"buttonCell"] autorelease];
  }
  UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
  [button setBackgroundColor:[UIColor redColor]];
  button.titleLabel.text = @"Foo Bar";
  [cell.contentView addSubview:button];

which produces a button, indeed. It doesn't look yet how it's supposed to (it's obvious I've never dealt with buttons in iPhone, yet), but is this at least the right approach?

+1  A: 

Yes, this is generally the correct approach.

A tip:

  • Set the callback for your button events, so that it actually does something when clicked.

    [myButton addTarget:self action:@selector(whatMyButtonShouldDo:)   
      forControlEvents:UIControlEventTouchUpInside];
    

EDIT: Adjusted code for using a system button, which renders a lot of what I had written uneccessary.

mmc
In his example code, there is no need to release the button, because [UIButton buttonWithType:] returns an autoreleased instance.
Daniel Rinser
You're exactly correct, I copied that from code *I* had where I used my initWithFrame. Adjusting.
mmc
Thanks for the feedback. The hint with release/autorelease was noted.Is it really necessary to fiddle with the frame? I simply want the button to fill the entire table cell (i.e not worry about position and size); "Delete Event" demonstrates this nicely.
Marcel
Yeah, I made things too complicated by copying my code, where I don't use System buttons, I set the frame and the background images by hand. I abbreviated my answer considerably.
mmc
A: 

Yes, you are on the right track, that's the easiest way to add a subview to a cell (the other is subclassing a UITableViewCell).

Check the Apple guide for more info.

Marco Mustapic
+4  A: 

The way you have it now each time a cell is shown you're allocating a button, setting its value, and adding it to the cell's contentView. When the cell gets reused (via dequeueReusableCellWithIdentifier) you'll be creating another new button, adding it to the cell (on top of the old one) etc. The fact that it's gone through addSubview but no explicit release means each button's retain count will never go to zero so they'll all stick around. After a while of scrolling up and down the cell will end up with hundreds of button subviews which probably isn't what you want.

A few tips:

  • Never allocate stuff inside a cellForRowAtIndexPath call unless it's done when dequeueReusableCellWithIdentifier is returning nil and you're initializing the cell. All other subsequent times you'll be handed back the cached cell that you will have already set up so all you have to do is change the labels or icons. You're going to want to move all that button allocation stuff up inside the if conditional right after the cell allocation code.

  • The button needs to have a position and also a target set for it so it'll do something when tapped. If every cell is going to have this button a neat trick is to have them all point to the same target method but set the button's tag value to the indexPath.row of the cell (outside the cell allocation block since it varies for each cell). The common tap handler for the button would use the tag value of the sender to look up the underlying data in the dataSource list.

  • Call release on the button after you've done an addSubview. That way the retain count will fall to zero and the object will actually get released when the parent is released.

  • Instead of adding the button via addSubview, you can return it as the accessoryView for the cell so you don't have to worry about positioning it (unless you're already using the accessoryView for something else -- like disclosure buttons).

Ramin
Thanks a bunch for the valuable hints and explanations. An iPhone programming newbie like me sure appreciates that.As for tip 4, as with "Delete Event"/"Share Contact" I'll only have one button at the bottom of the UI i.e. in the last row. So, it cannot be the accessoryView, but it has to fill the entire row. I'm still struggling with that.
Marcel
Regarding #3, releasing the button will crash your application. He isn't allocing it.
DougW
Nice write up Ramin!
Jordan