0
取消項目I有以下代碼:取消選擇,並從陣列
var selectedIndexPaths = [NSDate]()
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath) as? PartExpense
if cell?.selected == true {
cell?.layer.borderWidth = 4.0
cell?.layer.borderColor = UIColor.greenColor().CGColor
selectedIndexPaths.append(cell!.idPartecipant)
print(selectedIndexPaths)
}
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath)
if cell?.selected == false {
cell?.layer.borderColor = UIColor.clearColor().CGColor
if let index = selectedIndexPaths.indexOf(indexPath.row) {
selectedIndexPaths.removeAtIndex(index)
print(selectedIndexPaths)
}
}
}
一切順利直到行: 如果讓指數= selectedIndexPaths.indexOf(indexPath.row)
這是錯誤:「無法將Int類型的值轉換爲期望的類型NSDate」我該如何做到這一點?我如何轉換數組以避免此錯誤?
我需要把這個特殊的元素idPartecipant放在數組中,這是一個與coreData一起存儲的Nsdate。當我取消選擇該行時,我需要刪除該元素。 – user1679705
首先通過搜索數據的原始數據源(cellForItemAtIndexPath中使用的數組)來查找取消選擇的日期。一旦你的被取消選擇的一個,然後搜索你selectedIndexPaths陣列該日期 如果讓deselectedData:的NSDate = dataSouce [indexPath.row] { 如果讓指數= selectedIndexPaths.indexOf(deselectedData){ selectedIndexPaths.removeAtIndex(指數) } } –
與您的代碼,當我有時取消選擇我得到這個錯誤:「致命錯誤:索引超出範圍」 – user1679705