CollectionView cellForItemAt indexPath执行deque单元格但单元格视图为零

时间:2017-02-22 13:46:25

标签: ios swift3 uicollectionviewcell

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

1 个答案:

答案 0 :(得分:2)

首先使用标识符

创建自定义单元格的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"
}