简体   繁体   中英

CollectionView cellForItemAt indexPath does deque cells but cell's view is nil

guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifier", for: indexPath) as? MYVerticalCell else {
  fatalError("Couldn't deque `Vertical Cell`")
}

cell.view.backgroundColor = .black
return cell

view outlet is connected with cell

Throws unexpectedly found nil while unwrapping an optional value

First create xib of custom cell with identifier,

Register cell before use, using below example,

tableView.register(UINib(nibName: "custCell", bundle: nil), forCellReuseIdentifier: "custCell")

And then use below in "func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell"

var cell = tableView.dequeueReusableCell(withIdentifier: "custCell") as? CustCell

if (cell == nil) {
    cell = CustCell(style: UITableViewCellStyle.default, reuseIdentifier: "custCell"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM