繁体   English   中英

IOS 7 - 如何从放置在 UITableViewCell 中的按钮获取 indexPath

[英]IOS 7 - How to get the indexPath from button placed in UITableViewCell

我以编程方式创建了一个 UITableView 并将 UISwitch 添加到它的单元格附件视图中。

这是我在cellForRowAtIndexPath方法中的单元格附件视图中的 UISwitch 代码。

UISwitch *accessorySwitch = [[UISwitch alloc]initWithFrame:CGRectZero];
[accessorySwitch setOn:NO animated:YES];
[accessorySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = accessorySwitch;

这是单击按钮后调用的方法。

- (void)changeSwitch:(UISwitch *)sender{

    UITableViewCell *cell = (UITableViewCell *)[sender superview];

    NSIndexPath *indexPath = [self.filterTableView indexPathForCell:cell];
    NSLog(@"%ld",(long)indexPath);

 ……………. My other code…….
}

我能够在 iOS 6 中打印索引路径值但在 iOS 7 中它打印 nil,

我是否缺少 iOS 7 中的任何内容,或者还有其他一些方法可以在 iOS 7 中获取 indexPath

谢谢,阿伦。

NSLog(@"%ld",(long)indexPath); 是错误的,这将打印 indexPath 的指针地址

尝试使用以下代码

CGPoint center= sender.center; 
  CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.filterTableView];
  NSIndexPath *indexPath = [self.filterTableView indexPathForRowAtPoint:rootViewPoint];
  NSLog(@"%@",indexPath);

Swift解决方案:像这样的 UITableView 扩展对此很有用。

extension UITableView {
    func indexPathForView(view: AnyObject) -> NSIndexPath? {
        let originInTableView = self.convertPoint(CGPointZero, fromView: (view as! UIView))
        return self.indexPathForRowAtPoint(originInTableView)
    }
}

使用变得容易无处不在。

let indexPath = tableView.indexPathForView(button)

如果您在单元格中有按钮。 您可以通过调用 superview 来获取单元格。 然后可以通过这种方式获取Indexpath。

 (void)obButtonTap:(UIButton *)sender {

    UITableViewCell *cell = (UITableViewCell *)sender.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
}

您可以为 cellForRowAtIndexPath 方法中的每个开关分配标签,例如

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
   UISwitch *accessorySwitch = [[UISwitch alloc]initWithFrame:CGRectZero];
   [accessorySwitch setOn:NO animated:YES];
   [accessorySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
   cell.accessoryView = accessorySwitch; 
   accessorySwitch.tag = indexPath.row;    
}

- (void)changeSwitch:(UISwitch *)sender{
    NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:[sender tag]];
    NSLog(@"%ld",(long)indexPath);
}

快速 4:

extension UITableView {
    func indexPathForView(view: AnyObject) -> NSIndexPath? {
        let originInTableView = self.convert(CGPoint.zero, from: (view as! UIView))
        return self.indexPathForRow(at: originInTableView)! as NSIndexPath
    }
}

SWIFT 4 转换,创建全局扩展:

extension UITableView {
    func indexPathForView(view: UIView) -> IndexPath? {
            let originInTableView = self.convert(CGPoint.zero, from: (view))
            return self.indexPathForRow(at: originInTableView)
        }
}

用法示例:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = ...
cell.myButton.addTarget(self, action: #selector(myButtonAction(_:)), for: .primaryActionTriggered)
return cell

    }

@objc func myButtonAction(_ button:UIButton) {

        guard let ip = self.tableView.indexPathForView(view: button) else {
            return
        }
}

您可能会被烧毁,假设 switch 的superview将是tableViewCell 也许在 iOS 7 中他们改变了层次结构来实现某种视觉效果; 也许那里插入了一个新视图。

您可以子类化UISwitch并给它一个indexPath属性。 然后在您的cellForRowAtIndexPath ,将它分配到那里,以便您有一个参考。

我认为依靠按钮的位置来知道点击了哪个按钮是危险的。

我更喜欢创建一个字典,将按钮/开关本身作为键,并将 indexpath 作为值:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
...
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSValue * accessorySwitchKey = [NSValue valueWithNonretainedObject:[cell settingSwitch]];
    [[self switchIndexDictionary]setObject:indexPath accessorySwitchKey];
...
}

然后当开关/按钮被触发时,我很容易从我的字典中获取索引路径:

- (void)toggleSetting:(id)sender
{
    UISwitch *selectedSwitch = (UISwitch *) sender;

    NSValue * accessorySwitchKey = [NSValue valueWithNonretainedObject:selectedSwitch];
    NSIndexPath *indexPath = [[self switchIndexDictionary]objectForKey: accessorySwitchKey];
}

Swift5.x 上测试

如果您想在单击放置在 tableViewCell 中的按钮时获取行号,那么下面的函数将起作用。

 @IBAction func buttonClickeAction(_ sender: UIButton) {
      let btnPoint = sender.convert(CGPoint.zero, to: self.tableView)
      let indexPath = self.tableView.indexPathForRow(at: btnPoint)
      print(indexPath?.row)
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM