所以我想從我的數組中選擇對象,以便能夠在我進行IBAction時刪除它們。我想:從NSArray中選擇對象進一步用IBAction刪除
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (self.editEnabled) {
RDNote *selectedNote = [self.notes objectAtIndex:indexPath.row];
if (selectedNote.isSelected) {
selectedNote.selected = NO;
for (NSIndexPath *indexPathFromArray in self.indexPathsOfSelectedCells) {
if (indexPathFromArray.row == indexPath.row) {
[self.mutableCopy removeObject:indexPathFromArray];
}
}
} else {
selectedNote.selected = YES;
[self.indexPathsOfSelectedCells addObject:indexPath];
}
[self.collectionView reloadData];
IBAction爲:
- (IBAction)didTapTrashBarButton:(id)sender {
NSMutableArray *mutableNotes = [NSMutableArray arrayWithArray:self.notes];
for (NSIndexPath *indexPath in self.indexPathsOfSelectedCells) {
[mutableNotes removeObjectAtIndex:indexPath.row];
}
self.notes = [NSArray arrayWithArray:mutableNotes];
[self.collectionView performBatchUpdates:^{
[self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithArray:self.indexPathsOfSelectedCells]];
} completion:^(BOOL finished) {
self.indexPathsOfSelectedCells = nil;
[self activateEditMode:NO];
[self saveDataToFile:self.notes];
}];
}
但我有索引的問題,例如:(有時顯示我
,如果該項目被選中檢查對象索引2不在[0..1]之間的錯誤),並且在選擇多個對象並刪除它們時存在錯誤。 請幫我提供一些我可以使用的其他方法的建議,一個代碼將是完美的!謝謝!
什麼是'IBAction'中的'indexPath.row'?您在IBActions中傳遞UI對象,而不是索引路徑。 – trojanfoe
使用CoreData而不是mutableCopy。 –
我是新來的客觀請給我一個例子的鏈接,謝謝! –