如果用户选择按钮,我在 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/