Я не могу dequeueReusableCell, когда ячейка находится в собственном классе - PullRequest
0 голосов
/ 17 июня 2020

Я пытаюсь переместить свой код «ячейки» из функции «fun c tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath)» в свой собственный класс. Я зарегистрировал forCellReuseIdentifier и класс с помощью ViewController (я думаю). И я установил reuseIdentifier для ячейки прототипа на «MealSelector». В чем моя проблема?

Вот контроллер представления:

import UIKit

class MealSelectorController: UIViewController, UITableViewDelegate, UITableViewDataSource {


@IBOutlet weak var MealTable: UITableView!
@IBOutlet weak var ConfirmOrderButton: UIButton!
var recipes: [[String?]] = []


override func viewDidLoad() {
    super.viewDidLoad()

    for _ in 0 ..< 3 {
        let recipe: Recipe = APICaller.getNewRecipe()
        self.recipes.append([recipe.title, recipe.description])
    }

    MealTable.dataSource = self
    MealTable.delegate = self

    MealTable.register(MealSelectorCell.self, forCellReuseIdentifier: "MealSelector")
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.recipes.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MealSelector")

    return cell!
}

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

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}

}

Вот ячейка:

import UIKit

class MealSelectorCell: UITableViewCell {


override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

1 Ответ

1 голос
/ 17 июня 2020

В комментарии вы говорите:

Он вообще не использует мою ячейку-прототип для создания TableView

Если это означает ячейку-прототип в раскадровке , затем удалите эту строку:

MealTable.register(MealSelectorCell.self, forCellReuseIdentifier: "MealSelector")

Эта строка означает: не использовать прототип раскадровки. Так что вам нужно будет удалить его.

Вам также нужно будет установить класс ячейки прототипа в раскадровке.

Тем не менее, тогда я бы ожидал, что ваше приложение будет cra sh, потому что ваша реализация init(coder:) говорит cra sh.

fatalError("init(coder:) has not been implemented")

Возможно, вы тоже захотите это исправить ...

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