ios - Correct way to setup cell only once with a reusable cell? -
i'm looking correct way work reusable cell in swift.
how can make setup
method called once?
override func viewdidload() { super.viewdidload() self.tableview.register(uitableviewcell.self, forcellreuseidentifier: "awesomecell") } override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = self.tableview.dequeuereusablecell(withidentifier: "awesomecell") uitableviewcell? ?? uitableviewcell() self.setup(cell: cell) // want called once cell.textlabel?.text = self.data[indexpath.row] return cell } func setup(cell: uitableviewcell) { cell.accessorytype = .disclosureindicator }
thank you.
if register cell class, should put in init(style:reuseidentifier:)
initializer of cell. when call dequeuereusablecell(withidentifier:for:)
, initializer gets called table view cant reuse yet.
if registered cell xib, use cell's init(coder:)
Comments
Post a Comment