views:

56

answers:

4

I have UITable which contains some UIButtons. I would like a way to use the label.

The problem I have is that I need two tag labels, one for retrieving the cell in tableviewCellWithReuseIdentifier and configureCell.

Until recently I have been using the UIButton.title to determine which row in the table I selected. The (invisible) text was the row number.

Now I need to use the title for some visible text. How can I still find which row was pressed?

When I press the area outside the button the usual didSelectRowAtIndexPath is called. The hard part is that I want to capture both the UIButton (which calls its own selector) and the row, which I had been finding out from the title. Now I need the title for something else.

A: 

You could subclass UITableViewCell or UIButton and add some properties.

Ben
you dont need to do this. this is inefficient. You can use the delegate methods that UITableView provides so when a user selects a cell a delegate method is called. check my answer to this post.
Pavan
I think I misunderstood, I thought he was pressing the button rather than the cell. You are quite correct if he is selecting the cell.
Ben
A: 

You should implement the UITableViewDelegate protocol.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Do something based on selected row (indexPath.row)
}
aegzorz
This is already there, and it is called when I tap an area outside the UIButton. The thing is that I am trying to capture the button press which is in the cell *and* get the row as well.
John Smith
A: 

Implement the UITableViewDelegate protocol. It will help you and do what you need.

didSelectRowAtIndexPath is autmatically called whenever a row is selected in your table.

in your .m file the one with the table view controller if there isn't one there add this method:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

   // NSInteger row = [indexPath row];
   // or (indexPath.row)
   // do whatever you like with this value which tells you which row is selected.

}

PK

Pavan
This is already there, and it is called when I tap an area outside the UIButton.
John Smith
thats good. isnt that what you would like? Thats how you find out which row was pressed which was the question you presented in your post
Pavan
ok what you do, is in your table creation. there is a for loop that iterates and draws each cell, row by row, its this place you draw the button as well but give a value to the button, so that you can tell which button is which so when you call on that particular button you can fire off an event
Pavan
[cell.btn addTarget:self action:@selector(UpdateLabel:) forControlEvents:UIControlEventTouchDown];
Pavan
and you can change the button for each press
Pavan
I already have that. It can't tell me anything about the cell unfortunately. fluchpunkt is onto something.
John Smith
+2  A: 

I use something like this. This will get you the indexpath in the tableview. Depending on your cell-layout you have to change it.

- (IBAction)buttonAction:(id)sender {
    UIButton *button = (UIButton *)sender;
    UIView *contentView = [button superview];
    UITableViewCell *cell = [contentView superview];
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
    // shorter but not so obvious:
    // NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell*)[[sender superview] superview]];

    // do something
}
fluchtpunkt
Did I tell you that I love you?
John Smith
only the contentView line needs to be removed, and replace tableView with self in some cases.
John Smith
if you don't need the contentView then your cell layout is kind of wrong. You should add subviews to cell.contentView, so they are put in correct position if your cell changes into editing mode. But probably you are not using editing mode right now.
fluchtpunkt