从表视图控制器中的静态单元中启动一个方法

在我的代码中,我有一个静态单元格内的故事板的表格。 我试图在点击最后一个静态单元格时触发一个方法。

我应该在代码中写下什么来实现这一点。 如何在代码中引用静态单元而不发生错误。

在viewController中添加:

@property (nonatomic, weak) IBOutlet UITableViewCell *theStaticCell; 

将该sockets连接到故事板中的单元格。

现在在tableView:didSelectRowAtIndexPath方法中:

 UITableViewCell *theCellClicked = [self.tableView cellForRowAtIndexPath:indexPath]; if (theCellClicked == theStaticCell) { //Do stuff } 

使用静态单元,您仍然可以实现- tableView:didSelectRowAtIndexPath:并检查indexPath。 一种方法是用#define定义特定的indexPath,并检查select的行是否在该indexPath,如果是,则调用[self myMethod].

这里是我把混合静态和dynamic单元格时,

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if let staticIndexPath = tableView.indexPathForCell(self.staticCell) where staticIndexPath == indexPath { // ADD CODE HERE } } 

这避免了创build一个新的单元格。 我们都用来创build单元格,并在cellForRowAtIndexPathconfiguration

在CiNN的回答之后,这是解决这个问题的Swift 3版本。

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let staticIndexPath = tableView.indexPath(for: OUTLET_TO_YOUR_CELL), staticIndexPath == indexPath { // ADD CODE HERE } } 

这种方法允许不需要实现cellForRow方法,特别是如果您在故事板上使用静态单元格。