Имея два табличных представления в виде Swift - PullRequest
0 голосов
/ 11 июня 2018

Я пытаюсь иметь два табличных представления в одном представлении, я дал уникальный идентификатор для каждой ячейки, и у каждого табличного представления есть свой собственный класс ячейки.Коды

class ReportsViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var tableView2: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
 }
}

extension ReportsViewController : UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 1
}

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BreakingNewsTableViewCell

        return cell

}


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return 145
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Breaking News"
}

func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .long
    dateFormatter.timeStyle = .medium
    let date = Date()
    let dateString = dateFormatter.string(from: date)
    return "Updated on " + dateString
}

private func tableView2(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 2
}

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath) as! TopStoriesTableViewCell

    return cell

}


private func tableView2(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return 145
}

private func tableView2(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}

private func tableView2(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return " Top Stories"
}

private func tableView2(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .long
    dateFormatter.timeStyle = .medium
    let date = Date()
    let dateString = dateFormatter.string(from: date)
    return "Updated on " + dateString
}
}

Тем не менее, он продолжает сбой, как говорит, что моя ячейка не зарегистрирована?Могу ли я узнать, какую ошибку я здесь сделал?Я также связал делегатов табличного представления с собой

1 Ответ

0 голосов
/ 11 июня 2018

Вам нужно переключить все методы, как это

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

    if tableView  == self.tableView {

         let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BreakingNewsTableViewCell

         return cell
    }
    else {

          let cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath) as! cell2

          return cell

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