【问题标题】:tableview cell action issue xcodetableview 单元格操作问题 xcode
【发布时间】:2018-06-26 09:09:34
【问题描述】:

所以我正在创建这个待办事项应用程序。它在表格视图上。如果不清楚该项目是什么,则点击或单击每个单元格时都应将您带到 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)
        }

    }
}
  }

【问题讨论】:

    标签: ios swift xcode uitableview


    【解决方案1】:

    我认为这与搜索内容更改有关

     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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多