UIcollectionView限制item的交换
注意:其中的_optionsArr.count根据自己的需求为准
- (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)originalIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath {
if (proposedIndexPath.item == _optionsArr.count) {
return originalIndexPath;
} else {
return proposedIndexPath;
}
}
UITableView限制cell的交换
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
return sourceIndexPath;
} else {
return proposedDestinationIndexPath;
}
}
注意: 交换的cell或item的同时要注意数据源内数据的交换。
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
if (destinationIndexPath.item == _optionsArr.count) {
} else {
[self.optionsArr exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item
];
[self.dic setObject:self.optionsArr forKey:@"signArr"];
[self.dic writeToFile:plistPath atomically:YES];
[self.subscribeCollectionView reloadData];
}
}