didselectrowatindexpath textlabel return

Ale*_*. E 1 uitableview ios swift

我正在尝试从按下的单元格返回titleField或标题值.经过一些挖掘后,我发现了一些应该可行的代码,但是我得到了错误:

"UITableViewCell"类型的值没有成员'titleField'

对于标题,标签等所有其他项目也是如此.

extension SearchViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return posts.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier:"searchCell", for: indexPath)
        as! CustomTableViewCell
        cell.titleField?.text = posts[indexPath.row].caption
        cell.descriptionField?.text = posts[indexPath.row].description
        cell.tagsField?.text = posts[indexPath.row].tags
        let photoUrl = posts[indexPath.row].photoUrl
        let url = URL(string: photoUrl)
        cell.SearchImage.sd_setImage(with: url, placeholderImage: nil)
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        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 currentItem = currentCell.titleField!.text //HERE IS THE ERROR!
    }
}
Run Code Online (Sandbox Code Playgroud)

rma*_*ddy 5

切勿使用单元格来获取数据.从您的数据模型中获取数据cellForRowAt.

didSelectRowAt提供刚刚选择的行的索引路径.

将您更新didSelectRowAt为:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let caption = posts[indexPath.row].caption
    let tags = posts[indexPath.row].tags
}
Run Code Online (Sandbox Code Playgroud)