iOS UIcollectionView 和 UITableView 限制item 或cell的交换方法

UIcollectionView限制item的交换

注意:其中的_optionsArr.count根据自己的需求为准
- (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)originalIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath {
  /* 两个indexpath参数, 分别代表源位置, 和将要移动的目的位置*/
    if (proposedIndexPath.item == _optionsArr.count) {
        return originalIndexPath;
    } else {
        return proposedIndexPath;
    }
}

UITableView限制cell的交换

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    /* 两个indexpath参数, 分别代表源位置, 和将要移动的目的位置*/

    /* 判断两个indexPath参数的section属性, 是否在一个分区 */
    if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
        return sourceIndexPath;
    } else {
        return proposedDestinationIndexPath;
    }
}
注意: 交换的cell或item的同时要注意数据源内数据的交换。
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
    //交换资源数组中移动item和目标位置item的资源位置
    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];
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值