代码之家  ›  专栏  ›  技术社区  ›  Naval Hasan

从另一个TableView中的TableView访问按钮索引

  •  0
  • Naval Hasan  · 技术社区  · 7 年前

    我在另一个TableView中有一个TableView,内部TableView包含按钮。我可以在单击按钮时触发一个方法,但无法获取indexPath。当我使用下面的代码时,应用程序正在崩溃

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {   
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell 
        cell.myButton?.addTarget(self, action:#selector(myButtonPressed(_:)), for:.touchUpInside) 
        return cell   
    }
    
    @objc func myButtonButtonPressed(_ sender: AnyObject) {
        let button = sender as? UIButton
        let cell = button?.superview?.superview as? UITableViewCell
        let clasObj = myCell()
        let indexPath = myCell.InsidetabView.indexPath(for: cell!)
        let index = (indexPath?.row)!
        print(index) 
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Naval Hasan    7 年前

    我给按钮贴上标签 TableView, cellForRowAt indexPath buttonPressed 方法

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {   
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell 
        cell.myButton?.addTarget(self, action:#selector(myButtonPressed(_:)), for:.touchUpInside) 
    
        cell.button.tag = indexPath.row
    
        return cell   
    }
    
    @objc func myButtonButtonPressed(_ sender: AnyObject) {
        let button = sender as? UIButton
        let row = button!.tag
    }
    
    推荐文章