UitableView的增删和移动

12 篇文章 0 订阅
5 篇文章 0 订阅
//声明tableview并初始化Array
-(void)viewDidLoad{
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 50, 300, 350)];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];

NSMutableArray *dataArray = [[NSMutableArray alloc] initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7", nil];
}

//协议的重写
#pragma mark - UITableViewDataSource Methods -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return dataArray.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *Identifier = @"identifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier]; if (cell == nil) { cell = [[UITableViewCell alloc ]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]; } cell.textLabel.text = [dataArray objectAtIndex:indexPath.row]; return cell; } //对选中的行进行标记,在不编辑的状态下使用 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath]; if (cellView.accessoryType == UITableViewCellAccessoryNone) { cellView.accessoryType=UITableViewCellAccessoryCheckmark; } else { cellView.accessoryType = UITableViewCellAccessoryNone; [tableView deselectRowAtIndexPath:indexPath animated:YES]; } } //在进行下面的编辑之前先将列表设置为可编辑状态 [tableView setEditing:NO]; //实现列表的移动 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;//可移动 } //实现移动 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { // 需要的移动行 NSInteger fromRow = [sourceIndexPath row]; // 获取移动某处的位置 NSInteger toRow = [destinationIndexPath row]; // 从数组中读取需要移动行的数据 id object = [dataArray objectAtIndex:fromRow]; // 在数组中移动需要移动的行的数据 [dataArray removeObjectAtIndex:fromRow]; // 把需要移动的单元格数据在数组中,移动到想要移动的数据前面 [dataArray insertObject:object atIndex:toRow]; } //实现列表的编辑 //可编辑状态(增加行,逐行删除) - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; //设置为YES可滑动进入编辑状态 } //单元格返回的编辑风格,包括删除 添加 和 默认 和不可编辑三种风格 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;//返回编辑风格: -:删除; +:添加 无:不可编辑 } -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle==UITableViewCellEditingStyleDelete) { // 通过获取的索引值删除数组中的值 [dataArray removeObjectAtIndex:indexPath.row]; // 删除单元格的某一行时,在用动画效果实现删除过程 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];//动画:8种 } if(editingStyle==UITableViewCellEditingStyleInsert) { //增加行 } }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值