簡體   English   中英

CollectionView cellForItemAt indexPath可以使單元出隊,但單元的視圖為nil

[英]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

查看插座是否與電池連接

展開可選值時意外拋出nil

首先創建帶有標識符的自定義單元格的xib,

在使用之前注冊單元格,使用以下示例,

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

然后在下面的“ 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"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM