进击的UI--------------- UITableView&UITableView的移动

1.UITableView补充 
1⃣️:-(void)p_data
@property (nonatomic,retain)NSMutableArray *dataArray;
@property (nonatomic,assign)UITableViewCellEditingStyle editStyle;
- (void)p_data{
    NSMutableArray *arr1 = @[@"范冰冰",@"宋慧乔",@"周迅"].mutableCopy;
    NSMutableArray *arr2 = @[@"bangbangbang",@"boomshakalaka",@"comeonboys",@"comeongirls"].mutableCopy;
    self.dataArray = [NSMutableArray array];
    [self.dataArray addObject:arr1];
    [self.dataArray addObject:arr2];
}
2⃣️:number
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
3⃣️:row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray[section] count];
}
cell.textLabel.text = self.dataArray[indexPath.section][indexPath.row];
2.UITableView修改
1⃣️:让tableView处于编辑状态
- (void)p_navigation{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"SET" style:UIBarButtonItemStyleDone target:self action:@selector(rightAction:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"ADD" style:UIBarButtonItemStyleDone target:self action:@selector(leftAction:)];
}
- (void)leftAction:(UIBarButtonItem *)sender{
    self.editStyle = UITableViewCellEditingStyleInsert;
    [self.rv.tableView setEditing:!self.rv.tableView.editing animated:YES];
}
- (void)rightAction:(UIBarButtonItem *)sender{
    self.editStyle = UITableViewCellEditingStyleDelete;
    [self.rv.tableView setEditing:!self.rv.tableView.editing animated:YES];
}
2⃣️:指定可以编辑的行
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
3⃣️:指定tableView编辑的样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
        return self.editStyle;
}
4⃣️:完成编辑
①:修改数据流
1.[self.dataArray[indexPath.section] removeObjectAtIndex:indexPath.row];
2.[self.dataArray[indexPath.section] insertObject:@"New Peole" atIndex:indexPath.row + 1];
②:修改 UI
1[self.rv.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
2.NSIndexPath *newIndex = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
   [self.rv.tableView insertRowsAtIndexPaths:@[newIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
③:完整代码:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
       if (self.editStyle == UITableViewCellEditingStyleDelete) {
         // 1.修改数据源
    [self.dataArray[indexPath.section] removeObjectAtIndex:indexPath.row];
    // 2.修改UI
    [self.rv.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
       }else if (self.editStyle == UITableViewCellEditingStyleInsert){
           // 添加
           [self.dataArray[indexPath.section] insertObject:@"New Peole" atIndex:indexPath.row + 1];
           NSIndexPath *newIndex = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
           [self.rv.tableView insertRowsAtIndexPaths:@[newIndex] withRowAnimation:UITableViewRowAnimationAutomatic];}
3.UItableView移动
1⃣️:让tableView处于编辑状态
同修改
2⃣️:制定可以移动的行
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
3⃣️:完成移动
①:修改数据源
// 拿出数据
    NSString *temp = self.dataArray[sourceIndexPath.section][sourceIndexPath.row];
    // 从数组中删掉
    [self.dataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
    // 插入到指定的位置
    [self.dataArray[destinationIndexPath.section] insertObject:temp atIndex:destinationIndexPath.row];
②:修改UI
// 修改UI move方法
    [self.rv.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
③:完整代码:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    // 拿出数据
    NSString *temp = self.dataArray[sourceIndexPath.section][sourceIndexPath.row];
    // 从数组中删掉
    [self.dataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
    // 插入到指定的位置
    [self.dataArray[destinationIndexPath.section] insertObject:temp atIndex:destinationIndexPath.row];
    // 修改UI move方法
    [self.rv.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
④:不建议限制跨区域
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }else {
        return sourceIndexPath;
    }
}
4.UITableViewController
1⃣️: 先注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
2⃣️:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = @"123";
    return cell;
}

转载于:https://www.cnblogs.com/sharkHZ/p/4984112.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值