I've read a few posts but I could not find a clear answer to this question: in the MVC framework, who should be responsible for the UI set up? I'm balancing between two options:
class ViewController: UITableViewController {
private var users: [User]
...
override func tableView(
_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = dequeueDataCell(id: "myCell") as? CellView else { return UITableViewCell() }
cell.title.text = user?.name
cell.subtitle.text = user?.nickname
cell.action.isHidden = isSelected
// other numerous UI set up on the cell
return cell
}
}
class CellView: UITableViewCell {
@IBOutlet weak var title: UILabel?
@IBOutlet weak var subtitle: UILabel?
@IBOutlet weak var action: UIButton!
}
and
class ViewController: UITableViewController {
private var users: [User]
...
override func tableView(
_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = dequeueDataCell(id: "myCell") as? CellView else { return UITableViewCell() }
cell.user = self.users[indexPath.row]
cell.isSelected = false
cell.refresh()
return cell
}
}
class CellView: UITableViewCell {
@IBOutlet private weak var title: UILabel?
@IBOutlet private weak var subtitle: UILabel?
@IBOutlet private weak var action: UIButton!
weak var user: User?
var isSelected = false
func refresh() {
title.text = user?.name
subtitle.text = user?.nickname
action.isHidden = isSelected
// other numerous UI set up on the cell
}
}
I've so far been using the first approach that is found in most iOS tutorials. But I've found that this makes the UIViewController much longer, while they already have a tendency to become long and obscure, and harder to read since it mixes UI and state logic.
Aucun commentaire:
Enregistrer un commentaire