【问题标题】:call by touching cell in Swift在 Swift 中通过触摸单元格调用
【发布时间】:2017-10-15 09:16:30
【问题描述】:

我使用 Core Date 来保存姓名和电话号码 我想通过触摸手机拨打电话 这是我的代码: 导入 UIKit 导入核心数据

类视图控制器:UIViewController {

@IBOutlet weak var tableView: UITableView!

var people = [Person]()

override func viewDidLoad() {
    super.viewDidLoad()

    let fetchRequest: NSFetchRequest<Person> = Person.fetchRequest()
    do {
      let people = try PersistenceService.context.fetch(fetchRequest)
        self.people = people
        self.tableView.reloadData()
    } catch {}

}

@IBAction func onPlusTapped() {
    let alert = UIAlertController(title: "Add Person", message: nil, preferredStyle: .alert)
    alert.addTextField { (textField) in
        textField.placeholder = "Name"
    }
    alert.addTextField { (textField) in
        textField.placeholder = "Phone number"
        textField.keyboardType = .numberPad
    }
    let action = UIAlertAction(title: "Post", style: .default) { (_) in
        let name = alert.textFields!.first!.text!
        let phoneNumber = alert.textFields!.last!.text!
        let person = Person(context: PersistenceService.context)
        person.name = name
        person.phoneNumber = phoneNumber
        PersistenceService.saveContext()
        self.people.append(person)
        self.tableView.reloadData()

    }
    alert.addAction(action)
    present(alert, animated: true, completion: nil)
}

覆盖 var prefersStatusBarHidden: Bool { 返回真 } }

扩展视图控制器:UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { 返回 1 }

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
    cell.textLabel?.text = people[indexPath.row].name
    cell.detailTextLabel?.text = people[indexPath.row].phoneNumber
    return cell
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if (editingStyle == .delete) {
        people.remove(at: indexPath.item)
        tableView.deleteRows(at: [indexPath], with: .automatic)
    }
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    UIApplication.shared.openURL(NSURL(string: "tel://" + (people[indexPath.row].phoneNumber?.description)!)! as URL)
    print(people[indexPath.row].phoneNumber?.description)
}

}

【问题讨论】:

    标签: ios swift core-data tableview call


    【解决方案1】:

    不需要添加@IBAction,你可以使用来自UITableViewDelegate的didSelectRow

    【讨论】:

      【解决方案2】:

      如果UITableViewCell 内有 UIButton,则您已经在 UITableView 内实现 didSelectRowAt 使用 IBActions

      【讨论】:

        猜你喜欢
        • 2016-11-01
        • 2019-01-14
        • 2019-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多