在同一View Controller中使用2个表视图(Swift 4)

时间:2019-01-18 09:56:48

标签: swift uitableview

我在同一屏幕上处理2个表时遇到问题。每次他不断崩溃。有人可以帮我吗?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell: TableViewCellComunicazioni?

    if tableView == self.tableViewNotifica {
        cell = tableView.dequeueReusableCell(withIdentifier: "cellNotifica", for: indexPath) as? TableViewCellComunicazioni
        let dataNotifica = structNotifica[indexPath.row].dateNotifica
        let testoNotifica = structNotifica[indexPath.row].textNotifica

        cell?.dateNotification.text = "\(date!)"
        cell?.textNotification.text = "\(text!)"
        return cell!
    }
    if tableView == self.tableViewInbox {
        cell = tableView.dequeueReusableCell(withIdentifier: "cellInbox", for: indexPath) as? TableViewCellComunicazioni
        let email = structInbox[indexPath.row].email
        let messaggio = structInbox[indexPath.row].messaggio
        let data = structInbox[indexPath.row].data

        cell?.emailInbox.text = "\(email!)"
        cell?.messaggioInbox.text = "\(message!)"
        cell?.dataInbox.text = "\(date!)"
        return cell!
    }
    return UITableViewCell()
}

1 个答案:

答案 0 :(得分:1)

这可能是解决您问题的方法:

编码示例:

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

    if tableView == self.tableViewNotifica {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellNotifica", for: indexPath) as? TableViewCellComunicazioni
        let dataNotifica = structNotifica[indexPath.row].dateNotifica
        let testoNotifica = structNotifica[indexPath.row].textNotifica

        cell?.dateNotification.text = "\(date!)"
        cell?.textNotification.text = "\(text!)"
        return cell!
    }
    if tableView == self.tableViewInbox {
        let  cell = tableView.dequeueReusableCell(withIdentifier: "cellInbox", for: indexPath) as? TableViewCellComunicazioni
        let email = structInbox[indexPath.row].email
        let messaggio = structInbox[indexPath.row].messaggio
        let data = structInbox[indexPath.row].data

        cell?.emailInbox.text = "\(email!)"
        cell?.messaggioInbox.text = "\(message!)"
        cell?.dataInbox.text = "\(date!)"
        return cell!
    }
      return UITableViewCell()
    }

并确保您具有正确的要出队的单元格标识符。

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == moviesTableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MovieTableViewCell", for: indexPath) as! MovieTableViewCell
            cell.delegate = self
            cell.setupCell(listmoviesArray[indexPath.row],indexPath: indexPath)
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MovieTableViewCell2", for: indexPath) as! MovieTableViewCell
            cell.delegate = self
            cell.setupCell(listmoviesArray[indexPath.row],indexPath: indexPath)
            return cell
        }

    }

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if tableView == moviesTableView {
    // Handle your selection for row. 
    } else {
       //Handle your selection for row.
    }
  }
}

以上代码使用2个Tableview产生以下输出。

enter image description here