0

Possible Duplicate:
How to know the UITableview row number

I have a tableView and a tableViewCell on this tableView. I defined another class for the tableViewCell called CustomCell so that I coded the necessary customizations and also created a button (on this cell). When the button on a tableViewCell is clicked, I want to learn which tableViewCell contains that button so that I can make the necessary changes only to that cell (which contains the button clicked)

How can I understand that which tableViewCell contains the button that is clicked?

Community
  • 1
  • 1

6 Answers6

0

If you replace the accessory detail button with your custom button, then you can call the accessoryButtonTappedForRowWithIndexPath: method.

example - put this in cellForRowAtIndexPath (when setting up cell):

UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 125, 24)];
[myAccessoryButton setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
[cell setAccessoryView:myAccessoryButton];
[myAccessoryButton release];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

and then as a separate method:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:    (NSIndexPath *)indexPath
{
    //do something
}
joeByDesign
  • 133
  • 8
0

One strategy for implementing this is to assign a tag with the row number to the button that was pressed:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexpath {
    YourCustomCell *cell = // Code
    cell.button.tag = indexPath.row;

    // Other cell preparation code

    return cell;
}

Then, in the action for the button, you can see what its tag is to determine what model object that corresponds to:

- (void)didSelectButton:(id)sender {
    UIButton *button = sender;
    NSInteger tag = button.tag;

    YourModelObject *item = self.items[tag];

    //    Continue with the business logic for what should
    //    happen when that button is pressed.
}
Wayne Hartman
  • 18,369
  • 7
  • 84
  • 116
  • 1
    Tags works well until you start deleting cells. Then suddenly the tags no longer correspond to the correct model objects. – Bart Whiteley Jan 23 '13 at 18:36
0

How about this...

- (UITableViewCell *)cellWithSubview:(UIView *)subview {
    while (subview && ![subview isKindOfClass:[UITableViewCell self]]) subview = subview.superview;
    return (UITableViewCell *)subview;
}

- (IBAction)buttonClicked:(id)sender {
    UITableViewCell *theCell = [self cellWithSubview:sender];
}
danh
  • 62,181
  • 10
  • 95
  • 136
0

You need to call this cellForRowAtIndexPath method in this way.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(10 , 10, 200, 20);
    [button addTarget:self action:@selector(passRowValueMethod:) forControlEvents:UIControlEventTouchUpInside];
    [button setTag:indexPath.row];
    [button setTitle:[NSString stringWithFormat:@"%d", indexPath.row] forState:UIControlStateNormal];
    [cell addSubview:button];


    return cell;
}

Then define button target method and get tag value in this method.

-(void)passRowValueMethod:(UIButton*)sender
{
    UIButton *buttonGet = sender;
    NSLog(@"%d", buttonGet.tag);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Nirav Jain
  • 5,088
  • 5
  • 40
  • 61
0

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];

CGRect frame = CGRectMake(0.0, 10.0, 24, 24);
button.frame = frame;
[button setTag:((indexPath.section & 0xFFFF) << 16) |
 (indexPath.row & 0xFFFF)];
[button setImage:[UIImage imageNamed:@"link-buttoni4.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"link-button-onclicki4.png"] forState:UIControlStateHighlighted];
[button setSelected:NO];

// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet

[button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;

}

-(void)checkButtonTapped:(UIButton *)sender {

NSUInteger section = ((sender.tag >> 16) & 0xFFFF);
NSUInteger row     = (sender.tag & 0xFFFF);
NSLog(@"Button in section %i on row %i was pressed.", section, row);

}

Kaushal Bisht
  • 476
  • 2
  • 11
0

try this,
you can know section and row no of the cell which one is clicked in this method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
}

1>section can be get by
int sectionno = indexPath.section
2>cell index can be get by
int rowno = indexPath.row
and then using you can get the table cell like this,

UITableViewCell *cell=[product_table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowno inSection:sectionno]];
Banker Mittal
  • 1,918
  • 14
  • 26