如何从ios 9中tableview单元格中的按钮打开alertcontroller?

时间:2016-01-07 00:13:54

标签: uitableview swift2 ios9 cell uialertcontroller

我想打开一个带有呼叫按钮的Alertview,并从tableview单元格中的按钮取消按钮。

ffmpeg [input params] -i <input> [output params] <output>

这是customtableviewcell

var arrayOfUsernames: [PFObject] = [ ]

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayOfUsers.count
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int{
    return 1
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Tutors"
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell: CustomTableViewCell = TutorTable.dequeueReusableCellWithIdentifier("Tutor") as! CustomTableViewCell

    cell.CUsername.text = self.arrayOfUsers[indexPath.row]
    cell.Classes.text = self.arrayOfClasses[indexPath.row]
    return cell

}

2 个答案:

答案 0 :(得分:1)

@meteochu建议的方式可行,但我更喜欢protocol-delegate模式。

考虑到该按钮位于tableView单元格中,您可以使用委托,以便ViewController执行警报。以下是CustomtableViewCell中的协议:

protocol CustomTableViewCellDelegate: class {
   func customTableViewCell(customTableViewCell: cell, didTapButton button: UIButton)
}

然后在weak var delegate: CustomTableViewCellDelegate?

中添加一个属性CustomTableViewCell

之后,再次在您的CustomTableViewCell bookNow行动中,您可以像这样解雇代表:

self.delegate.customTableViewCell(self, didTapButton: sender as! UIButton) // Or just 'sender', if you set your action type to UIButton

最后,在您的tableView所在的ViewController中,确保通过实现其协议订阅CustomTableViewCell的委托:

// Note the CustomTableViewCellDelegate here
class ViewController: CustomTableViewCellDelegate, UITableViewDelegate, UITableViewDataSource {
   // ...
   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      let cell: CustomTableViewCell = TutorTable.dequeueReusableCellWithIdentifier("Tutor") as! CustomTableViewCell
      // ...
      cell.delegate = self
      return cell
   }

   // Implementing the protocol function
   func customTableViewCell(customTableViewCell: cell, didTapButton button: UIButton) {

      let phone = "+888888888888" // Sample phone number

      // Alert code
      let alertController = UIAlertController(title: "Call \(phone)", message:
        "Are you sure you want to perform this call?", preferredStyle: .Alert)

      // Call Button
      alertController.addAction(UIAlertAction(title: "Call", style: .Destructive) { action in
      // Code for making a call
      if let url = NSURL(string: "tel://\(phone)") {
          UIApplication.sharedApplication().openURL(url)
      } else
      })

      alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))  // Cancel button
      self.presentViewController(alertController, animated: true, completion: nil)  // Show the alert on the screen, here the ViewController does the presenting
   }
}

正如您所看到的,为了创建提醒,您需要使用UIAlertController,使用UIAlertControllerStyle.Alert样式调用它以获得正确的标题和消息。

然后,您只需使用.addAction添加操作(按钮),然后在完成处理程序中键入要执行的代码。最后,您需要记住使用presentViewController显示提醒。

如果您的号码更改,协议会引用您可用于传递单元格属性的单元格。

请参阅UIAlertViewController课程的官方文档。

答案 1 :(得分:1)

由于UITableViewCell是一个UIView子类,而不是UIViewController,因此您需要创建一个包含您可以呈现的viewController的变量。

首先,您要为A 456.456.456.456 subdomain 123.123.123.123 添加weak viewController var。我建议使用CustomTableViewCell,因为如果您需要其他内容,可以使用navigationController,如果需要,可以从中控制堆栈。如果没有,您可以使用UITableViewController。

pushViewController:方法中,只需将单元类中的cellForRowAtIndexPath:变量设置为当前的navigationController或tableViewController:

navigationController 要么... cell.navigationController = self.navigationController

一旦这样做,在cell.viewController = self方法中,您现在可以使用UIViewController来呈现视图。这是代码的样子......

您的UITableViewController类:

bookNow:

在CustomCellClass中:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) → UITableViewCell? {
  let cell = tableView.dequeueReusableCellWithIdentifier("Tutor") as! CustomTableViewCell
  // setup your cell here
  cell.navigationController = self.navigationController
  // cell.viewController = self
  return cell
}

另外,我强烈建议将变量名称更改为小写。大写字母通常用于weak var navigationController: UINavigationController? // weak var viewController: UIViewController? @IBAction func bookNow(sender: AnyObject) { let alertController = UIAlertController(/* setup */) // setup alertController self.navigationController?.presentViewController(alertController, animated: false, completion: nil) // self.viewController?.presentViewController(alertController, animated: false, completion: nil) } 名称