swift - 在同一个 View Controller 中使用 2 个 TableView (Swift 4)

标签 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()
}

最佳答案

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

代码示例:

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

关于swift - 在同一个 View Controller 中使用 2 个 TableView (Swift 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54251473/

相关文章:

ios - Firebase 数据库查询出错

swift - Firebase Firestore 查询未执行

ios - map 上的自定义图钉(注释)动画

ios - 如何停止在 TableView 中编辑?

ios - 在 UITableView 中禁用删除但启用移动单元格

ios - 修改 UITableView 中 didSelectRowAtIndexPath 上的单元格

ios - 保存图像然后在 Swift (iOS) 中加载它

swift - 从一个 UIStoryBoard Controller 切换到另一个 UIStoryBoard Controller

ios - 在 TableViewCell Swift 中从 Firebase 加载图像

iphone - 折叠 UITableView