如何知道UITableView中Tableview单元格按钮上的indexPath /行? [英] How to know the indexPath/row on button click of Tableview cell in a UITableView?

查看:98
本文介绍了如何知道UITableView中Tableview单元格按钮上的indexPath /行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个具有自定义UITableViewCell的TableView。每个tableview行都有一个按钮。现在我想知道单击按钮时的行号,这样我就知道单击了哪个行按钮。我已尝试在堆栈上找到一些东西,但没有任何工作。

I created a TableView having a custom UITableViewCell. A button is associated with each row of tableview. Now I want to know the row number on click of a button, so that I would know that of which row button has been clicked. I have given a try to few things found on stack but nothing is working.

我试过这段代码 - :

I have tried this code -:

-(void)button1Tapped:(id)sender
{
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *buttonCell = (UITableViewCell *)[senderButton superview];
UITableView* table = (UITableView *)[buttonCell superview];
NSIndexPath* pathOfTheCell = [table indexPathForCell:buttonCell];
NSInteger rowOfTheCell = [pathOfTheCell row];
NSLog(@"rowofthecell %d", rowOfTheCell);
}

但这也不起作用。

感谢您帮助我。

推荐答案

试试这个

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

这篇关于如何知道UITableView中Tableview单元格按钮上的indexPath /行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆