Используйте 2 табличных представления в одном контроллере представления (Swift 4) - PullRequest
0 голосов
/ 18 января 2019

У меня проблема с обработкой 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 голосов
/ 18 января 2019

Это может быть вероятное решение вашей проблемы:

Пример кодирования:

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 таблицами.

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...