2017-02-27 98 views
1

我有一个的tableview单元内的按钮(如图所示的下面的图像中),当我点击按钮内,didSelectRowAtIndexPath方法不触发按钮的tableview细胞夫特的iOS

didSelectRowAt indexpath

没有被触发,有人可以建议我如何做到这一点?

请注意: 我执行一组按钮的点击动作,另外我也想

didselectRowAt indexPath

被触发。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
print("Table view cell has been clicked") 
} 

enter image description here

+0

您是否将TableView委托分配给您要放入代码的类?你应该使用UITableViewDelegate –

+0

按钮的功能是什么?它可以直接调用'didselectRowAt indexPath'(或者更准确地说,调用'didselectRowAt indexPath'调用的其他函数以及按钮自己的方法)。看看:http://stackoverflow.com/questions/20117163/didselectrowatindexpath-is-not-invoked-to-uibutton-of-cellforrowatindex –

+0

可能重复的[didSelectRowAtIndexPath不被调用到cellForRowAtIndex的UIButton](http:///stackoverflow.com/questions/20117163/didselectrowatindexdex-is-not-invoked-to-uibutton-of-cellforrowatindex) – 2017-02-27 01:23:33

回答

0

把出口到按钮的自定义类细胞内。然后在cellForItemAt中为button设置标签并添加选择器。这个例子是为colletionView只是改变以适应tableView。

如果你想在每个单元格中的按钮,这是你如何做到这一点。向单个单元添加一个静态按钮不会调用didSelectItemAt,因为您点击的按钮没有引用可重用单元indexPath。

这样我们发送button.tag到他的功能,所以我们知道按钮与哪个单元格有关。

class MyClassViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 

    .... // Stuff 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    ....// Create Cell 

    cell.deleteCellButton.tag = indexPath.item 
    cell.deleteCellButton.addTarget(self, action: #selector(MyClassViewController.deleteCellButtonTapped(_:)), for: .touchUpInside) 
    return cell 
} 

func deleteCellButtonTapped(_ sender: Any) { 

    ... // Stuff 

     print("Selector called") 
} 
} 
+0

请勿将索引路径用作标签。它在许多情况下失败。 – rmaddy

+0

我读过的苹果不想让你再使用它。那么解决方案是什么?因为这似乎是大多数人构建嵌套表视图/集合视图的方式? – WanderingScouse

+1

有很多问题需要报道,所以我不会提供太多细节。但基本上,如果您需要确定按钮操作方法中按钮的索引路径,则可以使用UITableView类的方法基于按钮相对于表视图的框架计算它。 – rmaddy

0

如果要添加上UITableViewCell按钮或手势,didselectRowAt indexPath不被调用。您可以在剩余UI UITableViewCell上添加按钮,而不是在按钮上应用清除颜色。用户没有显示按钮,您可以执行didselectRowAt indexPath方法任务。如果你想在按钮方法代码indexpath下面给出。

func btnChildDropDown(sender:UIButton) 
    { 
     let cell = sender.superview?.superview as! ChildAgeTableViewCell 
     let indexPath = self.tblViewAddRooms.indexPath(for: cell) 

    } 
相关问题