Swift 4 错误:无法赋值:"btn"是一个"let"常量



我正在尝试创建一个代码,该代码将返回UICollectionViewUITableView内按钮的背景图像。这是代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt 
indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: 
"InsideCollectionViewCell", for: indexPath) as! InsideCollectionViewCell
if let btn = cell.viewWithTag(100) as? UIButton {
btn = collectionPics[indexPath.row]
}
return cell
}

看起来您正试图在集合视图单元格中设置图像。假设:

  • 收藏图片:[UIImage]
  • btn设置正确

然后您可以更换

btn = collectionPics[indexPath.row]

带有(编辑:用于背景图像(

btn.setBackgroundImage(collectionPics[indexPath.row], for: .normal)

最新更新