【问题标题】:Get indexPath.row in IBAction?在 IBAction 中获取 indexPath.row?
【发布时间】:2011-11-23 21:38:55
【问题描述】:

在我的应用中有一个表格视图。选择表视图中的行时,将出现UIView 987654321并显示信息。 行标题来自一个 plist 文件,其中包含字符串。 plist 文件还包括带有与行标题关联的电话号码的字符串。 在自定义 UIView 我有一个按钮,当您单击该按钮时,我希望它调用 plist 文件中声明的数字。 与用户单击的行关联的数字。

我怎样才能做到这一点?

动作是一个普通的IBAction:

- (IBAction)callNumber:(id)sender;

它连接到 IB 中的按钮(在 Xcode 4 中)。

如果有人能解决我的问题,我将不胜感激,谢谢。

编辑

为了清楚起见,我想获取与您在表格视图中选择的行相关联的电话号码。 plist 中的字符串有一个键,用于电话号码名称:“电话”。

NSString *phone = [[tableList objectAtIndex:indexPath.row] objectForKey:@"phone"];

“tablelist”是一个 NSMutableArray。我想获取密钥“电话”并将其存储在 NSString *电话中。所有这些都在 IBAction 中。发布整个课程会有所帮助吗?

【问题讨论】:

    标签: objective-c uibutton plist ibaction nsindexpath


    【解决方案1】:

    对此有一个优雅的解决方案,那就是设置UITableViewCell。将 UIButton 的标签设置为行的索引:

    - (UITableViewCell *)tableView:(UITableView *)tableView 
             cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString* CellIdentifier = @"Cell";
    
        YourCustomCell* cell = (YourCustomCell*)[tableView 
                                  dequeueReusableCellWithIdentifier:CellIdentifier];
    
        //  All the code for loading a cell
    
        //  Other code you have for configuring the cell
    
        //  HERE'S THE IMPORTANT PART: SETTING THE
        //  BUTTON'S TAG TO THE INDEX OF THE ROW
        cell.phoneButton.tag = indexPath.row;
    
        return cell;    
    }
    

    然后,在动作代码中,您可以将索引拉出标签:

    - (IBAction)callNumber:(id)sender {
        UIButton* button = sender;
        int index = button.tag;
        NSString *phone = [[tableList objectAtIndex:indexPath.row] objectForKey:@"phone"];
        //  Make the phone call
    }
    

    【讨论】:

    • 这看起来很有希望,但我认为我不太了解UITableViewCell 部分。
    【解决方案2】:

    您需要在“自定义视图”上有一个属性,该属性指示所选行的索引路径。您可以将其放在自定义视图控制器的标题中以声明此属性:

    @interface MyCustomViewController : UIViewController {
        ...
        NSIndexPath * indexPath;
    }
    @property (nonatomic, retain) NSIndexPath * indexPath;
    

    然后,像这样设置实现:

    @implementation MyCustomViewController
    @synthesize indexPath;
    ...
    // only needed if not using ARC
    - (void)dealloc {
        self.indexPath = nil;
        ...
        [super dealloc];
    }
    @end
    

    现在,在tableView:didSelectRowAtIndexPath: 方法中,像往常一样简单地创建视图控制器,但在其上设置indexPath 属性以便将来可以访问它:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        MyCustomViewController * vc = [[MyCustomViewController alloc] initWithBundle:nil nibName:nil];
        [vc setIndexPath:indexPath];
        [self presentModalViewController:vc];
        // if not using ARC
        [vc release];
    }
    

    然后,在MyCustomViewController.m 的任何位置,只需写self.indexPath.row 即可获取被选中的索引路径所在的行。

    【讨论】:

    • 感谢您的快速回答。看起来不错,但我想在同一个视图控制器中获取选定的行,“自定义视图”是同一个视图控制器中的 UIView。那怎么办?感谢您的帮助!
    • 在这种情况下,只需将 indexPath 属性放在主视图控制器上,然后将self.indexPath = indexPath 放在tableView:didSelectRowAtIndexPath: 中。然后,使用self.indexPath 访问当前选择的索引路径。
    • 非常感谢!现在完美运行。
    【解决方案3】:

    作为一种快速的临时解决方法,您可以执行以下操作:

    单元格选择方法: - 保存选择 indexPath.row 的 NSUserDefault

    在您的下一个视图中: - 读取读取的任何行的 NSUserDefault 值

    这将使下一个视图知道选择了哪个视图,并允许您为其获取正确的数据。

    【讨论】:

      【解决方案4】:

      如果您在 UITableViewCell 中有执行需要知道索引路径的操作的控件,则可以使用以下几种方法:

        1234563然后在调用该操作时,检索 sender.tag - 1 并获得行索引。
      1. 从您的控制中沿着 superview 链向上走,直到到达 UITableViewCell。调用[tableView indexPathForCell:cell 获取indexPath。

      2. 创建控件的子类并将 indexPath 存储在控件中。

      【讨论】:

        猜你喜欢
        • 2017-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-20
        • 1970-01-01
        • 1970-01-01
        • 2015-04-23
        相关资源
        最近更新 更多