【问题标题】:CollectionView cellForItemAt indexPath does deque cells but cell's view is nilCollectionView cellForItemAt indexPath 确实使单元格成双列,但单元格的视图为零
【发布时间】:2017-02-22 13:46:24
【问题描述】:
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与cell相连

在展开可选值时意外发现 nil

【问题讨论】:

  • 代码在哪一行崩溃?
  • @Mansi Shahuse control+k 格式化您的代码。
  • @Mansi Shah 在您的自定义单元格类中有 UIView 吗?您是否在 viewDidLoad 中使用了某些东西来注册您的单元格?
  • 只是检查。你确定是 Vertical Call 而不是 VerticalCell?
  • 检查插座视图是否与 MYVerticalCell 正确连接。

标签: ios swift3 uicollectionviewcell


【解决方案1】:

首先创建带有标识符的自定义单元格的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"
}

【讨论】:

  • 我的问题是 UICollectionView 无论如何,我已经完成了相同的注册,并且也使用集合视图单元格出列。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
相关资源
最近更新 更多