我有一个带有自定义单元格的tableView。在每个单元格中,都有一个UILabel,它从NSMutableArray获取文本。我设置了一个GestureRecognizer来查看此标签是否被点击。如果它被点击,我转到另一个视图。

现在,在与标签选择器关联的方法中,我想获取NSMutableArray中文本的索引,该索引对应于被轻击的标签。我该怎么办?

这是一些销售代码:
在cellForRowAtIndexPath:方法中,这是我的工作:

    cell.Label.text = [tableLabelText objectAtIndex:indexPath.row];

    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToViewControllerB:)];

    [cell.Label setUserInteractionEnabled:YES];
    [cell.Label addGestureRecognizer:tap];
    [tap release];


然后我展示一切...

这是goToViewControllerB方法:

- (void) goToViewControllerB:(id)sender {
    if (!viewControllerB) {
    viewControllerB = [[ViewControllerB alloc] initWithNibName:@"ViewControllerB" bundle:nil];
}
    navigationController = [[UINavigationController alloc] initWithRootViewController:viewControllerB];
    [self presentModalViewController:navigationController animated:YES];
}


如果这样做,并且在viewControllerB中修改了某些内容并返回到“表格视图”,然后单击另一个标签,则将指向相同的viewControllerB,其中包含先前的修改。我确实想为每个标签显示一个新的viewControllerB。

我的想法是获取与所敲击的Label相对应的tableLabelText的索引。每当我调用goToViewControllerB:时,我都可以创建一个ViewControllerB类型的新对象,并将其插入到NSMUtableArray中,并且索引相同。

if ([ViewControllerArray objectAtIndex: "here the index i'm looking for"]){
"show it" }
else {
"create it and insert it into ViewControllerArray at the right index" }

最佳答案

使用数组的indexOfObject方法。

关于ios - 如何获取TableView中被点击对象的索引?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11243848/

10-10 03:26