ios - 在我们自己的声明方法中如何获取单元格选择行呢?

标签 ios objective-c uitableview segue

如果用户选择按钮,我在 UITableView 中有按钮,那么它必须转到 NextViewController [携带一些 JSON 值]。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    orderid=[NSString stringWithFormat:@"%@",[[arrDictionary objectAtIndex:indexPath.row]objectForKey:@"orderid"]];
    NSLog(@"orderid==%@",orderid);
}

我可以轻松地从所选单元格中获取值

我在 UITableViewCell 中有按钮,所以我需要让按钮点击来自哪一行?

我尝试使用这段代码,但它只显示我存储的第一个 orderid

-(void)nextview:(UIButton *)button
{
    NSIndexPath *index=[tableview indexPathsForSelectedRows];
    orderid=[NSString stringWithFormat:@"%@",[[arrDictionary objectAtIndex:index]objectForKey:@"orderid"]];
    NSLog(@"orderid==%@",orderid);
}

最佳答案

如果您没有使用多项选择,那么您可以获得一行,该行由 :

NSIndexPath *index = [tableview indexPathForSelectedRow];

如果您有多行选择,则:

您将获得选定索引的选定 NSIndexPath 数组,如下所示:

NSArray *selectedIndexPaths = [tableview indexPathsForSelectedRows];

您可以运行循环直到 selectedIndexPaths.count 并获取选定的行数据。


文档:

// Selection

- (NSIndexPath *)indexPathForSelectedRow;
// returns nil or index path representing section and row of selection.
- (NSArray *)indexPathsForSelectedRows NS_AVAILABLE_IOS(5_0); 
// returns nil or a set of index paths representing the sections and rows of the selection.

关于ios - 在我们自己的声明方法中如何获取单元格选择行呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32268042/

相关文章:

ios - 如何在类别中创建 UITableViewCell 明智地排列 JSON 对象

iphone - 在 UITableViewCell 的 subview 上添加单击和双击手势

ios - UIView 后面的黑色,而 UIView 转换动画

ios - 除了 cellForRowAtIndexPath 之外,在哪里设置 UITableViewCell ? objective-C

objective-c - UIView 模态弹出窗口带有嵌入式 UITableView 和 UIToolbar,工具栏随表格移动

ios - 将 Zip 文件添加到项目并获取位置

ios - Swift 字符串作为 UIColor 对象

iphone - UITableView 顶部的 UISearchBar 可以隐藏但靠近 UINavigationBar

ios - FBSDKShareLinkContent 应用处于实时状态时无法共享标题和描述 iOS

ios - 表行加载时应用程序崩溃