xcode 从 TableView Objective C 中获取 IndexPath.Row

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20927451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:30:26  来源:igfitidea点击:

Get IndexPath.Row from TableView Objective C

iosxcodetableviewcellnsindexpath

提问by Swag

Before asking this question, I searched a lot on Google and Stackoverflow. Tried also some examples, but I can't make the function work.

在问这个问题之前,我在谷歌和 Stackoverflow 上搜索了很多。还尝试了一些示例,但我无法使该功能正常工作。

Since the hierarchy of the tableview is changed since iOS 7, is it kind of hard to find a solution.

由于自 iOS 7 以来 tableview 的层次结构发生了变化,因此很难找到解决方案。

I got a standard tableview with a couple items and one button on the screen.

我得到了一个标准的 tableview,屏幕上有几个项目和一个按钮。

I need to get the indexPath.row number when selecting an item from the tableview and clicking on the button.

从 tableview 中选择一个项目并单击按钮时,我需要获取 indexPath.row 编号。

This is my code

这是我的代码

- (IBAction)buttonGetNumber:(id)sender {

    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[(UIView *)[button superview] superview]];
    NSLog(@"%i", indexPath.row);
}

This keeps returning a '0', no matter which item I select from the tableview.

无论我从 tableview 中选择哪个项目,这都会返回“0”。

I also tried this (2):

我也试过这个(2):

- (IBAction)buttonGetNumber:(id)sender {
    UIButton *button = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *) [[button superview] superview];
    NSIndexPath *index = [self.tableView indexPathForCell:cell];
    NSLog(@"%i", index.row);
}

This also returns a '0'.

这也返回“0”。

I also tried this (3):

我也试过这个(3):

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

And this makes the application crash.

这会使应用程序崩溃。

Any clue how I can solve this?

有什么线索可以解决这个问题吗?

enter image description here

在此处输入图片说明

回答by kkocabiyik

Create an instance variable _lastClickedRowSet it with tableview delegate like below. And when you click the to "Get Row" button use _lastClickedRow.

创建一个实例变量_lastClickedRow使用 tableview 委托设置它,如下所示。当您单击“获取行”按钮时,请使用 _lastClickedRow。

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

    _lastClickedRow = indexPath.row;
}

- (IBAction)buttonGetNumber:(id)sender {

    NSLog(@"%d" , _lastClickedRow);
}

回答by coverback

If for some reason selected cell doesn't work for you (e.g. for a multiple selection case), you could get row from sender's frame:

如果由于某种原因选定的单元格对您不起作用(例如,对于多选情况),您可以从发件人的框架中获取行:

- (IBAction)buttonGetNumber:(id)sender
{
    CGPoint buttonOrigin = sender.frame.origin;
    CGPoint pointInTableview = [self.tableView convertPoint:buttonOrigin fromView:sender.superview];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInTableview];
    if (indexPath) {
        // Do your work
    }
}

回答by n00bProgrammer

You simply set the tagof the UIButtonsame as the indexPath.rowusing:

您只需设置tag了的UIButton一样的indexPath.row使用:

yourButton.tag = indexPath.row;

in didSelectRowForIndexPath:.

didSelectRowForIndexPath:.

Then in buttonGetNumber: method, you get the row number using:

然后在 buttonGetNumber: 方法中,您使用以下方法获取行号:

int rowNum = [(UIButton*)sender tag];

Here, you have the advantage of not using any third variable.

在这里,您的优势是不使用任何第三个变量。