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

How to get the cell selection row in our own declaration method?

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

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

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

我在 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.

您可以获得选定的单元格作为

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]]; 

和 indexPath 为 [self.tableView indexPathForSelectedRow]

而且我认为您随后会进行 segue

    [self perfromSegueWithIdentifier:@"Segue"];

用于推送 json 到 NextViewController

-(void)YouButtonSelector:(UIButton *)sender{
NextViewController *rightController=[[NextViewController alloc] init];
rightController.eventIdIs=self.event_id;
rightController.PreviousJson=PresentJson;
rightController.hidesBottomBarWhenPushed=YES;
[self.navigationController pushViewController:rightController animated:YES];

}

点击按钮

 [cell.checkboxBtn setTag:indexPath.row];
 [cell.checkboxBtn addTarget:self action:@selector(nextview:) forControlEvents:UIControlEventTouchUpInside];

  -(void)nextview:(UIButton *)button
  {
  NSLog(@"Button select is :%d",button.tag);
  }

1 - 您可以在 cellForRowAtIndexPath 方法中提供按钮标签

button.tag = indexPath.row;

2 - 您已收到订单ID

-(void)nextview:(UIButton *)button
{

NSString *orderid=[NSString stringWithFormat:@"%@",[[arrDictionary objectAtIndex:button.tag] objectForKey:@"orderid"]];

NSLog(@"orderid==%@",orderid);

}

分配

cell.btn.tag = indexPath.row;

和按钮按下功能使用

从数组中获取数据
UIButton *btn = (UIButton *)sender;
NSString *str = arrDictionary[btn.tag];