当知道ios中uitableview的indexPath.row时,如何获取哪个uibutton



我有一个uitableview,它每行有两个uibutton(button1和button2)。现在我知道indexPath.row(例如:indexPath.row=2)。当我有uitableview的IndexPath.row时,如何获取button1?请给我任何解决这个问题的建议。提前谢谢。

我用了这个代码:

-(void)UpdateBtn:(NSNotification *)notification {
    //Update your button image code here
    shotIDPass =[[NSUserDefaults standardUserDefaults]valueForKey :@"videoIDstring"];
    NSLog(@"%@",shotIDPass);
    for (int i =0; i < [shotIDArray count]; i++) {
        if([[shotIDArray objectAtIndex:i] isEqualToString:shotIDPass])
        {
             NSLog(@"IndexPath of row %@",i); // get indexPath.row at here
        }
    }
}

您需要创建一个自定义单元格类。然后使用tableview委托方法。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell) {
        cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
return cell;
}

完成后,添加按钮上的操作。

您可以在xib或故事板中执行此操作以链接到IBOutlet,也可以在MyCustomCell.m 中以编程方式执行此操作

只需将IBOutlet作为您的按钮,在方法中您只需分配执行选择器,如

   -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 [cell.button1 addTarget:self action:@selector(button1:) forControlEvents:UIControlEventTouchUPInside];
   [cell.button2 addTarget:self action:@selector(button2:) forControlEvents:UIControlEventTouchUPInside];

  return cell;
  }

然后你就可以实现你的点击按钮的方法,比如这个

-(IBAction)按钮1:(UIButton*)btn

   { 
     //Your Implementation
     }

与第二个按钮相同

必须设置每个按钮标记

在这里我解释了如何设置按钮标签并找到点击按钮的index.row

  • 首先,您需要创建一个自定义单元格类需要定义两个按钮
  • 而不是在您需要创建一个NSMutableArray以及需要在表视图中添加多少行之后,即所有单元格都添加到阵列中

-(IBAction)clickEvent:(UIButton *)sender; -(void)viewDidload{ NSMutableArray *arrayCellCollection = [[NSMutableArray alloc] init]; int tagCount=1; for (int i =0; i < [shotIDArray count]; i++){ AddPropertyCell *cell = [[AddPropertyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifiour"]; cell.button1.tag=tagCount; [cell.button1 addTarget:self action:@selector(clickEvent:)forControlEvents:UIControlEventTouchUpInside]; tagCount++; cell.button2.tag=tagCount; [cell.button2 addTarget:self action:@selector(clickEvent:)forControlEvents:UIControlEventTouchUpInside]; tagCount++; [arrayCellCollection addObject:cell]; }} -(IBAction)clickEvent:(UIButton *)sender {int getIndexRow =0; int a = sender.tag %2; if (a==0) { getIndexRow=((a/2)-1); } else { getIndexRow=(a/2); } } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { return [arrayCellCollection objectAtIndex:indexPath.row]; }

最新更新