Re: Design for custom tableviewcell button action
Re: Design for custom tableviewcell button action
- Subject: Re: Design for custom tableviewcell button action
- From: WT <email@hidden>
- Date: Thu, 25 Jun 2009 10:16:41 +0200
Hi Bryan,
I had exactly the same problem and here's how I solved it. I have a
custom table view cell in a separate nib file whose File Owner's class
is the table view controller class. I then have an IB action defined
in the table view controller class and I link the button in the cell
nib to the action in the table view controller by linking the button
to the cell's File Owner's action instead. Of course, I still need to
attach the table view controller object to each actual cell's File
Owner, which I do here:
- (UITableViewCell*) tableView: (UITableView*) table_view
cellForRowAtIndexPath: (NSIndexPath*) index_path
{
static NSString* cellID = @"cellID";
CustomCell* cell = (CustomCell*) [table_view
dequeueReusableCellWithIdentifier: cellID];
if (cell == nil)
{
NSArray* nib = [[NSBundle mainBundle]
loadNibNamed: @"CellNibName" owner: self options: nil];
cell = (CustomCell*) [nib objectAtIndex: 0];
// other one-time cell configuration stuff here
}
// per-cell configuration stuff here
}
Note the two key steps:
1) the File Owner proxy object in the custom cell's nib file must have
its class set to your table view controller class.
2) you must pass 'self' as the owner in the -
loadNibNamed:owner:options: call.
This way, when the button is tapped, the action in the table view
controller is triggered. Now, presumably, you know which cell is
currently selected (you probably keep track of that in your table view
controller class), so you always know which cell the button action
came from.
Alternatively, you can set the button's tag to an index that depends
on the cell's index path. If your table has only one section, then
button.tag = [index_path row];
would do be sufficient. You should set the tag inside the if (cell ==
nil) block above, assuming that you expose the button as a cell
property and assuming that you're not adding or deleting cells. If you
do add or delete cells, you should set the button tag *outside* the if
(cell == nil) block. Either way, you can identify which row (ie, which
cell) is responsible for triggering the action by looking at the
action's sender's tag.
Wagner
On Jun 25, 2009, at 8:30 AM, Bryan Hansen wrote:
I'd like to add a custom button to my own custom tableview subclass
that will perform an action on the tableviewcontroller class. This
is pretty much identical to the way accessoryviews call a method on
the tableviewdelegate when it is tapped. The difference is it will
be my own button placed where I choose in the cell. The problem I'm
having is figuring out how to propagate this to the
tableviewcontroller. The UITableViewCell class does not hold a
reference back to the tableview or controller, so I'm a little
confused on the best way to set up this behavior. Can anyone offer
some insight on the correct way to get a button tap in a cell to
call a method on the tableviewcontroller? One that does not have bad
coupling in its design?
Thanks,
Bryan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden