简体   繁体   English

tableview单元动作问题xcode

[英]tableview cell action issue xcode

So I am creating this todo app. 因此,我正在创建此待办事项应用程序。 It is on a tableview. 它在表格视图上。 And each cell when tapped or clicked should take you to a ask.com search to search for the item if it is not clear what the Item is. 如果不清楚该项目是什么,则在点击或单击每个单元格时,应带您进入ask.com搜索以搜索该项目。 I have gotten it to search on ask.com with the code I have written. 我已经用我编写的代码在ask.com上进行搜索了。 But the issue that I have coming up is that after the first click. 但是我遇到的问题是第一次单击后。 The page doesn't refresh or update. 该页面不会刷新或更新。 I can click on the second or third cell and it wont search for what is in that particular cell. 我可以单击第二个或第三个单元格,它不会搜索该特定单元格中的内容。 It keeps showing what is in the first cell. 它一直显示第一个单元格中的内容。 and won't change. 并不会改变。 I have tried clearing cells and it still keeps going through as the old search from the first time. 我已经尝试清除单元格,并且仍然是第一次进行旧的搜索。 Ex: Cell 1 : cleaning products Cell 2: a bike Cell 3: dog. 例如:单元格1:清洁产品单元格2:自行车单元格3:狗。 No matter what cell I pick it will only show cleaning product. 无论我选择哪个单元,它都只会显示清洁产品。 Even if I change cell 1 to another item. 即使我将单元格1更改为另一个项目。 How can I fix this. 我怎样才能解决这个问题。 Source code would be amazing. 源代码将是惊人的。

  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, didSelectRowAt 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)
        }

    }
}
  }

I think it relates to search content change this 我认为这与搜索内容的变化有关

 let currentCell = tableView.cellForRow(at: indexPath!)! as UITableViewCell

to

 let currentCell = tableView.cellForRow(at: indexPath!) as! NewTableViewCell

&&& change this &&&更改此

 let TODO = currentCell.textLabel!.text

to

let TODO = currentCell.label.text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM