Tableviewcells动作XCode

时间:2018-06-23 10:25:37

标签: ios swift uitableview

所以我目前正在制作TODO应用。当前存在使我的tableView单元可点击或“可点按”的问题。我运行模拟器,似乎代码无法完全正常工作。我一直点击每个单元格,但它什么也没做。我想要的是每个单元格中的每个TODO项都可单击,并转到ask.com进行搜索,就像在我的代码中一样。但似乎该特定功能无法正常工作,我不知道该怎么办。如果有人可以为我解决此问题,那就太好了。源代码将是惊人的。提前致谢。我也是新手。我将“ **”我认为是错误的代码并且不起作用的部分。我尝试使用'didSelectRowAt'委托,如果这是正确的,也可能是由于代码的位置,但我不确定。我包括了所有相关内容。谢谢

import UIKit
class NewTableViewController: UITableViewController, NewCellDelegate, {
    var news:[News]!

    override func viewDidLoad() {
        super.viewDidLoad()

        loadData()

        func loadData() {
            news = [News]()
            news = DataManager.loadAll(News.self).sorted(by: {$0.createdAt < $1.createdAt})
            self.tableView.reloadData()

        }

        @IBAction func Save(_ sender: Any) {
            let addAlert = UIAlertController(title: "ADD", message: "TODO", preferredStyle: .alert)
            addAlert.addTextField { (textfield:UITextField) in
                textfield.placeholder = "TODO"
            }

            addAlert.addAction(UIAlertAction(title: "Save", style: .default, handler: { (action:UIAlertAction) in
                guard let title = addAlert.textFields?.first?.text else {return}
                let newsave = News(title: title, completed: false, createdAt: Date(), itemIdentifier: UUID())
                newsave.saveItem()
                self.news.append(newsave)

                let indexPath = IndexPath(row: self.tableView.numberOfRows(inSection: 0), section: 0)

                self.tableView.insertRows(at: [indexPath], with: .automatic)


            }))

            addAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

            self.present(addAlert, animated: true, completion: nil)

        }

    };

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return news.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! NewTableViewCell
        cell.delegte = self

        let news = self.news[indexPath.row]

        cell.label.text = news.title

        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:
            NSIndexPath) {
        //getting the index path of selected row
        let indexPath = tableView.indexPathForSelectedRow

        //getting the current cell from the index path
        let currentCell = tableView.cellForRow(at: indexPath!)! as UITableViewCell

        //getting the text of that cell
        let TODO = currentCell.textLabel!.text

        let appURL = NSURL(string: "https://www.ask.com/web?q=\
            (TODO))&o=0&qo=homepageSearchBox)")

        if UIApplication.shared.canOpenURL(appURL! as URL) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(appURL! as URL, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(appURL! as URL)
            }

        }
    }
}

2 个答案:

答案 0 :(得分:3)

您使用了错误的didselect方法,请使用下面的一种

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{'your code'}

答案 1 :(得分:0)

使用此方法:

func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath)

并设置其委托和数据源,

tableView.delegate =自我 tableView.dataSource = self

相关问题