Ios 从UITableViewCell子类调用UIActionSheet

Ios 从UITableViewCell子类调用UIActionSheet,ios,swift,uitableview,subclass,uiactionsheet,Ios,Swift,Uitableview,Subclass,Uiactionsheet,我正在制作一个表单,其中有一堆不同的UITableViewCells,具有不同的函数,因此,我不想在UITableView类中为每个动作使用ode,而是想在cells子类本身上实现每个函数 所以,我现在有一个单元格,其中会弹出一个UIActionSheet,但当我点击时,什么都不会发生,即使已设置了委托 我的想法错了吗?用另一种方法会更好吗?下面是一些简单的代码 import UIKit class STypeCell: UITableViewCell, UIActionSheetDelegat

我正在制作一个表单,其中有一堆不同的UITableViewCells,具有不同的函数,因此,我不想在UITableView类中为每个动作使用ode,而是想在cells子类本身上实现每个函数

所以,我现在有一个单元格,其中会弹出一个UIActionSheet,但当我点击时,什么都不会发生,即使已设置了委托

我的想法错了吗?用另一种方法会更好吗?下面是一些简单的代码

import UIKit
class STypeCell: UITableViewCell, UIActionSheetDelegate {

    func chooseType() {
        var sheet: UIActionSheet = UIActionSheet()
        let title: String = "Please choose a course"
        sheet.delegate = self
        sheet.title  = title
        sheet.addButtonWithTitle("Cancel")
        sheet.addButtonWithTitle("A course")
        sheet.addButtonWithTitle("B course")
        sheet.addButtonWithTitle("C course")
        sheet.cancelButtonIndex = 0
        sheet.showInView(self.superview)
    }

    func actionSheet(sheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
        println("this never gets called")
    }

    func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int) {
        println("this never gets called")
    }

    func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {
        println("this never gets called")
    }

TableViewCell中不应该包含这种逻辑

如果要重用该逻辑,则应创建一个TableViewController(或ViewController),并将其子类化。 在TableView委托方法中,您将调用您的操作

例如:

class MySuperTableView: UITableViewController, UIActionSheetDelegate {

// [...]

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
  {
      self.chooseType()
  }

  func chooseType() {
      var sheet: UIActionSheet = UIActionSheet()
      let title: String = "Please choose a course"
      sheet.delegate = self
      sheet.title  = title
      sheet.addButtonWithTitle("Cancel")
      sheet.addButtonWithTitle("A course")
      sheet.addButtonWithTitle("B course")
      sheet.addButtonWithTitle("C course")
      sheet.cancelButtonIndex = 0
      sheet.showInView(self.view)
  }

  func actionSheet(sheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
      println("this never gets called")
  }

  func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int) {
      println("this never gets called")
  }

  func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {
      println("this never gets called")
  }

// [...]
}

class MyChildTableView: MySuperTableView {

// [...]

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
  {
      super.tableView(tableView, didSelectRowAtIndexPath: indexPath)
  }

// [...]

}

class MyOtherChildTableView: MySuperTableView {

// [...]

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
  {
      super.tableView(tableView, didSelectRowAtIndexPath: indexPath)
  }

// [...]

}

当然,更多的逻辑可能会有帮助:点击了什么样的行?我是否有其他可点击的TableViewCell,因此我应该检查

尝试在KeyWindow中显示动作视图您需要添加一个按钮或一些其他触摸事件动作来生成您的动作表。快速问一下,你是在用细胞吗?如果是这样的话,我建议创建一个函数来处理此请求,该请求是从具有该表的视图控制器上的DidSelectRowatineXpath生成的。我的tableview工作得非常好,我显示的只是其中一个单元格的子类。在didSelectRowAtIndexPath中,我检查STypeCell类,如果是这样,我在我的单元格中调用函数chooseType(),该函数显示操作表,但不会在@thefreelement触发其委托。我认为您需要在
中设置
委托
覆盖
STypeCell
类中的funct awakeFromNib()
方法,它应该可以工作,但是,公认的答案是首选的方式。如上所述,除了从我的操作表中获得回调之外,一切都正常,这意味着我的tableview工作正常。但是你说了一件关键的事情,我要求,如果uitableviewcell应该包含这种逻辑,那么你说它不应该包含这种逻辑。谢谢你,我会另找一条路的,不客气。尽量保持你的观点“沉默”。他们负责展示模型中的数据。管制员负责前后反映变化。:)这是一种bs,视图控制器应保持精简,视图应实现所有视图功能,以使其可重用。如果我在社交应用程序中按照您的方式进行操作,则我必须为所有方法复制多种类型的方法我显示状态的各种方式,理想情况下,我应该能够实现一些使用状态的东西,并且它附带了所有状态功能。