Я пытаюсь вернуть значение 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!
}
}