UI之单个表视图的移动插入和删除


  [btn addTarget:self action:@selector(doEdit:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem*item = [[UIBarButtonItem alloc]initWithCustomView:btn];

    self.navigationItem.rightBarButtonItem= item;

    [self createTableData];

}

- (void)doEdit:(UIButton*)btn{

    self.tableV.editing= !self.tableV.editing;

   

    if (self.tableV.editing) {

        [btn setTitle:@"完成" forState:UIControlStateNormal];

    }else{

        [btn setTitle:@"编辑" forState:UIControlStateNormal];

    }

}

//点击delete按钮会调用这个方法

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"编辑");

    //先把当前数据源删除

    [self.dataArr removeObjectAtIndex:indexPath.row];

    //再刷新tableV

    [self.tableV reloadData];

}

//可以滑动删除可以交换两个行的位置

- (void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath{

    NSLog(@"%@  %@",sourceIndexPath,destinationIndexPath);

    //交换两个元素的在数据源中的位置

//    [self.dataArrexchangeObjectAtIndex:sourceIndexPath.rowwithObjectAtIndex:destinationIndexPath.row];

    if(destinationIndexPath.row >sourceIndexPath.row) {//上向下移

        //先插入

        [self.dataArr insertObject:self.dataArr[sourceIndexPath.row] atIndex:destinationIndexPath.row+1];

        //后删除

        [self.dataArr removeObjectAtIndex:sourceIndexPath.row];

    }else{

         [self.dataArr insertObject:self.dataArr[sourceIndexPath.row] atIndex:destinationIndexPath.row];

        [self.dataArr removeObjectAtIndex:sourceIndexPath.row+1];

    }

    //最后刷新

    [self.tableV reloadData];

}

//选中tableView中的某一行时,触发此方法

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

    NSLog(@"selected section:%ld row:%ld",(long)indexPath.section,(long)indexPath.row);

}

//某一行被反选时,触发此方法

- (void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath{

    NSLog(@"deselect section:%ld row:%ld",(long)indexPath.section,(long)indexPath.row);

}

 //设置cell的选中样式,iOS7之前,cell的选中样式默认为蓝色,iOS7之后

    cell.selectionStyle= UITableViewCellSelectionStyleGray;

    //设置cell右侧的提示样式

    //UITableViewCellAccessoryDisclosureIndicator箭头提示

    cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;

    //将数据,赋值给cell

    //indexPathsection带有该行所在的分区信息 row 该行在对应分区的位置

    //取到该分区所使用的数组

    NSArray*array = [_dataArray objectAtIndex:indexPath.section];

    NSString*str = [array objectAtIndex:indexPath.row];

    //将数组赋值给cell

    //UITableViewCellStyleSubtitle副标题才能显示

    //设置cell的主标题

    cell.textLabel.text = str;

    //设置副标题

    cell.detailTextLabel.text = @"test";

    //设置图片(头像)

    cell.imageView.image = [UIImageimageNamed:@"0.png"];

    returncell;

}

//设置分区的头标题

- (NSString *)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{

    return [NSString stringWithFormat:@"%ld分区头标",'A'+section];

}

//设置分区角标

- (NSString *)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section{

    return [NSString stringWithFormat:@"%ld分区角标",'A'+section];

}

//编辑调用

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath{

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值