我需要帮助使每个单元的地址唯一。当我更改单元格1的背景色时,单元格4的背景色也会更改。检查单元格描述后,我发现我的单元格使用相同的3地址。单元1的地址与单元4、7、10 ...相同,单元2的地址与单元5、8、11 ...相同,单元3与单元6、9、12 ...相同。 >
我希望每个单元格都是唯一的,但我想为每个单元格重复使用相同的单元格类型。
我的收藏查看协议代码:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return noOfQuestions!
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellid, for: indexPath) as! QuestionCell
cell.starPageLabel.text = String(indexPath.row + 1)
cell.addImageButton.tag = indexPath.item
cell.addImageButton.addTarget(self, action: #selector(handleAddImage), for: .touchUpInside)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
currentPage = Int(targetContentOffset.move().x / view.frame.width)
}
@objc func handleAddImage () {
print("Current Page: \(currentPage!)")
let cell = collectionView.cellForItem(at: IndexPath(row: currentPage!, section: 0)) as! QuestionCell
print(cell.description)
cell.questionTextField.backgroundColor = .red
}