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];

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


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;
    @interface MyTableViewCell: UITableViewCell
    @property (nonatomic, weak) id< MyTableViewCellDelegate> delegate; 
  • 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

Need Your Help

MySQL and performance

mysql sql performance sql-update

I am effecting 50,000+ updates on a MySQPL table fairly frequently. I tried it this way:

link_to using with active records

ruby-on-rails-3 activerecord link-to

The link to the user name to its profile in a table. Using active records i am finding the name and id but not able to correctly link it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.