collectionView가 있습니다. 모든 셀에는 버튼 actionButton
이 포함되어 있습니다. 버튼에 부착 된 타겟을 통해 버튼을 제거하려면 removeItem
메소드가 있어야합니다. datas
에는 컬렉션 항목이 있습니다.컬렉션 셀 내의 버튼에서 인덱스가 잘못되었습니다. 삭제 셀 뒤의 셀
override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
super.collectionView(collectionView, willDisplay: cell, forItemAt: indexPath)
guard let cell = cell as? ViewCell else { return }
let index = indexPath.row % datas.count
let item = datas[index]
cell.item = item
cell.actionButton.tag = indexPath.item
cell.actionButton.addTarget(self, action: #selector(removeItem), for: .touchUpInside)
}
컬렉션보기에서 항목을 삭제하는 방법이 있습니다.
@objc func removeItem(sender: UIButton) {
let indexPath = IndexPath.init(item: sender.tag, section: 0)
self.datas.remove(at: indexPath.item)
collectionView?.deleteItems(at: [indexPath])
}
그러나 컬렉션 셀에서 삭제 된 항목 버튼 인덱스는 다시로드되지 않습니다. 예를 들어, 인덱스가 [0, 0] 인 1 번째 항목을 다음 (2 번째) 항목으로 제거하면 1-st가되지만 버튼 인덱스는 여전히 [0, 1]입니다.
내가 뭘 잘못하고 왜 단추 인덱스가 다시 정렬되지 않습니다?
콜렉션 뷰를 다시로드해야합니까? – AlexWoe89
'collectionView? .deleteItems (at : [indexPath])'다음에'collectionView.reloadData()'를 추가하려고했으나 도움이되지 않았습니다. – andrey
관련이 없지만'collectionView'는 선택 사항입니까? – rmaddy