首页 > 解决方案 > 预测tableView swift 4

问题描述

我有 2 个 tableView 和 2 个 tableView 首先显示预测文本的数据和秒,我使用 textField 作为 searchBar 所以当我为 sec tableView 设置单元格时,当我尝试在cellForRowAt方法返回单元格时给我那个错误

无法将类型的返回表达式转换UITableViewCell.Type为返回类型UITableViewCell

这就是我的代码

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if tableView == tableView {
    let cell = tableView.dequeueReusableCell(withIdentifier: "SearchCell", for: indexPath) as! searchCell

    //cell.pics = receiveData[indexPath.item]

    cell.titleCell.text = receiveData[indexPath.row].adeTitle
    cell.cityCell.text = receiveData[indexPath.row].city
    cell.dateCell.text = receiveData[indexPath.row].date
    cell.distanceCell.text = receiveData[indexPath.row].distance
    cell.priceCell.text = receiveData[indexPath.row].adePrice

    if receiveData[indexPath.row].typ == "2" {
        cell.kindCell.text = "used"
        cell.kindCell.backgroundColor = UIColor.darkGray
        cell.kindCell.textColor = UIColor.white

    } else if receiveData[indexPath.row].typ == "1" {
        cell.kindCell.text = "New"
    } else {
        cell.kindCell.text = "none"
        cell.kindCell.backgroundColor = UIColor.darkGray
        cell.kindCell.textColor = UIColor.white
      }
    } else {
        var cell = searchTableView.dequeueReusableCell(withIdentifier: "Cell" )
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
        }
        cell?.textLabel?.text = inputs[indexPath.row]
    }
  return UITableViewCell
  } 

标签: iosswiftuitableview

解决方案


看起来您的方法UITableViewCell中没有返回 ' 实例,cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
  if tableView == tableView {
     let cell = tableView.dequeueReusableCell(withIdentifier: "SearchCell", for: indexPath) as! searchCell
     ...
     // do your stuffs with cell
     ...
     return cell
  }
  return UITableViewCell() 
}

推荐阅读