簡體   English   中英

tableview單元動作問題xcode

[英]tableview cell action issue xcode

因此,我正在創建此待辦事項應用程序。 它在表格視圖上。 如果不清楚該項目是什么,則在點擊或單擊每個單元格時,應帶您進入ask.com搜索以搜索該項目。 我已經用我編寫的代碼在ask.com上進行搜索了。 但是我遇到的問題是第一次單擊后。 該頁面不會刷新或更新。 我可以單擊第二個或第三個單元格,它不會搜索該特定單元格中的內容。 它一直顯示第一個單元格中的內容。 並不會改變。 我已經嘗試清除單元格,並且仍然是第一次進行舊的搜索。 例如:單元格1:清潔產品單元格2:自行車單元格3:狗。 無論我選擇哪個單元,它都只會顯示清潔產品。 即使我將單元格1更改為另一個項目。 我怎樣才能解決這個問題。 源代碼將是驚人的。

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

    }
}
  }

我認為這與搜索內容的變化有關

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

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

&&&更改此

 let TODO = currentCell.textLabel!.text

let TODO = currentCell.label.text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM