Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 获取自定义单元格上的节号和行号按钮单击?_Iphone_Objective C_Cocoa Touch - Fatal编程技术网

Iphone 获取自定义单元格上的节号和行号按钮单击?

Iphone 获取自定义单元格上的节号和行号按钮单击?,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我有一个带有自定义单元格的tableview。表格分为许多部分和行。我在单元格上有一个自定义按钮。现在,当我点击那个按钮时,我想得到节号和行号。? 如果您对此有任何想法,请在tableView中选择单元格时调用以下方法: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 要访问节号:int section=indexPath.section 要访问行号(在正确

我有一个带有自定义单元格的tableview。表格分为许多部分和行。我在单元格上有一个自定义按钮。现在,当我点击那个按钮时,我想得到节号和行号。?
如果您对此有任何想法,请在tableView中选择单元格时调用以下方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
要访问节号:
int section=indexPath.section

要访问行号(在正确的部分中):
int row=indexPath.row

UITableView:

一些想法:

可以遍历按钮的superview层次结构,直到找到UITableViewCell,然后在UITableView上调用-(NSIndexPath*)indexPathForCell:(UITableViewCell*)单元格

- (void)buttonClicked:(id)sender {
  UIView *button = sender;

  for (UIView *parent = [button superview]; parent != nil; parent = [parent superview]) {
    if ([parent isKindOfClass: [UITableViewCell class]]) {
      UITableViewCell *cell = (UITableViewCell *) parent;           
      NSIndexPath *path = [self.tableView indexPathForCell: cell];

      // now use the index path

      break; // for
    }
  }
}
您也可以使用按钮的标记来存储引用行的索引。这只保存一个整数,因此当您只有一个节,或者将行作为平面列表管理时,它最有意义


您也可以将UITableViewCell子类化以封装按钮。UITableViewCell可以响应按钮事件,并将事件重新广播给自己的代理,传递self。然后,事件委托可以在UITableView上调用-(NSIndexPath*)indexPathForCell:(UITableViewCell*)单元格以获取索引路径。

添加UITableViewCell子类的can实例变量以存储单元格的索引路径:

NSIndexPath *myIndexPath;
在中创建单元时:

cellForIndexPath:
将indexpath传递给新创建/回收的单元格


现在,当您按下按钮时,只需从单元格的ivar读取indexpath。

您需要在视图控制器上实现UIControl事件处理方法,并将其设置为所有按钮的处理程序。i、 e.在
-tableView:cellforrowatinexpath:
函数中,您可以执行以下操作:

[theCell.button addTarget: self
                   action: @selector(buttonPressed:withEvent:)
         forControlEvents: UIControlEventTouchUpInside];
那么您的事件处理程序将如下所示:

- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{
    UITouch * touch = [[event touches] anyObject];
    CGPoint location = [touch locationInView: self.tableView];
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];

    /* indexPath contains the index of the row containing the button */
    /* do whatever it is you need to do with the row data now */
}

轻微的修正。。。[event Touchs]应该是[event AllTouchs],当用户点击一行上的按钮时不会调用它;只有当他们点击行背景本身时才会调用它。
- (void) buttonPressed: (id) sender withEvent: (UIEvent *) event
{
    UITouch * touch = [[event touches] anyObject];
    CGPoint location = [touch locationInView: self.tableView];
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];

    /* indexPath contains the index of the row containing the button */
    /* do whatever it is you need to do with the row data now */
}