How can we get row and section no of cell if cell is having button

i have groped table view and each section i have two rows in that rows i have multiple buttons suppose i have clicked on section 0 in first cell button how can i get section no and row no of that cell.

Here is my cellforrow, I am getting row no but how to find out section no ??

- (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 contentView] setBackgroundColor:[UIColor clearColor]];
        [[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
         [cell.Mybutton addTarget:self action:@selector(btnCommentClick:) forControlEvents:UIControlEventTouchUpInside];
    }
     cell.Mybutton.tag=indexPath.row;
}

-(void)btnCommentClick:(id)sender
{
        UIButton *senderButton = (UIButton *)sender;  
        NSLog(@"current Row=%d",senderButton.tag);
        NSIndexPath *path = [NSIndexPath indexPathForRow:senderButton.tag inSection:0];
}

Answers


One way to properly handle this is as follows. N.b. if you think this is a lot of extra and unnecessary work, you are either very lazy and a fan of Very Bad Design, or you are not getting how to set up smoothly designed OO code.

  • create a custom UITableViewCell subclass
  • set the target of the buttons to be the cell
  • in the subclassed UITableVIewCell's .h file, create a protocol and a weak property on the tableViewCell that conforms to that protocol e.g.:

    @class MyTableViewCell
    
    @protocol MyTableViewCellDelegate
    - (void) leftButtonTappedOnTableViewCell: (MyTableViewCell *) cell;
    - (void) rightButtonTappedOnTableViewCell: (MyTableViewCell *) cell;
    @end
    
    @interface MyTableViewCell: UITableViewCell
    @property (nonatomic, weak) id< MyTableViewCellDelegate> delegate; 
    @end
    
  • Make sure that the selector in the tableViewCell that gets called in respons to the buttons call one of the delegate methods, on the delegate property of the cell.

  • Back in you viewController's cellForRowAtIndexPath, you should make sure that you use your UITableVIewCell subclass, and set the delegate to be the UIViewController. Then, implement the two delegate methods, and you can get the indexPath for the cell by asking the tableVIew for it:

    - (void) rightButtonTappedOnTableViewCell: (MyTableViewCell *) cell{
         NSIndexPath * indexPath = [self.tableView indexPathForCell: cell];
        // ... do something now that you now the indexPath
    }    
    

Try this:

- (void) btnCommentClick:(id)sender
{
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath != nil)
    {
     ...
    }
}

You can the section from indexPath.section


The tableViewCell is a superview somewhere in the view hierarchy. You can iterate through the superviews to find the tableViewCell.

- (UITableViewCell *)cellForButton:(UIView *)button {
    if ([button.superview isKindOfClass:[UITableViewCell class]])
        return button.superview; // Return found cell
    else if (button.superview)
        return [self cellForButton:button.superview]; // Find next superview
    else
        return nil; // No cell found
}

When found you can get the indexPath by:

UITableViewCell *cell = [self cellForButton:yourButton];
IndexPath *indexPath = [tableView indexPathForCell:cell];

You can try by giving tag value for cell and other property like accessibilityIdentifier for section like below..

- (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 contentView] setBackgroundColor:[UIColor clearColor]];
    [[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
     cell.Mybutton.tag = indexPath.row;
     cell.Mybutton.accessibilityIdentifier = [NSString stringWithFormat:@"%d",indexPath.section];
     [cell.Mybutton addTarget:self action:@selector(btnCommentClick:) forControlEvents:UIControlEventTouchUpInside];
       }

}

-(void)btnCommentClick:(id)sender
{
    UIButton *senderButton = (UIButton *)sender;  
    NSLog(@"current Row=%d current section =%d",senderButton.tag,[senderButton.accessibilityIdentifier intValue]);

}

Need Your Help

@Entity with JSF, persist "hello world" with JPA

java jsf jpa ejb cdi

Oracle describes very well how to make an @Entity. However, it's not strictly clear to me how to actually add/drop tables. I also like the rose india explanation, but just want to clarify the gen...

What is 'JNI Global reference'

java swing jprofiler

I am using jProfiler to find memory leaks in a Java swing application. I have identified instances of a JFrame which keeps growing in count.