UITableViewCell的移动、插入与删除

来源:互联网 发布:手机淘宝店铺模板复制 编辑:程序博客网 时间:2024/05/16 06:01

当我们的工程中需要动态插入或者删除UITableViewCell时,我们该如何做呢?

如果要实现这些操作,前提要实现移动单元格就需要把单元格的编辑属性设置为YES,有两种方式,第一种是改变tableview的属性:[tableView setEditing:YES animated:YES];,第二种通过代理的方式:

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}

1.移动

//返回YES,表示支持单元格的移动  -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath  {      return YES;  }  
//单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格  -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  {      return UITableViewCellEditingStyleInsert;  } 
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath  {  //    需要的移动行      NSInteger fromRow = [sourceIndexPath row];  //    获取移动某处的位置      NSInteger toRow = [destinationIndexPath row];  //    从数组中读取需要移动行的数据      id object = [self.listData objectAtIndex:fromRow];  //    在数组中移动需要移动的行的数据      [self.listData removeObjectAtIndex:fromRow];  //    把需要移动的单元格数据在数组中,移动到想要移动的数据前面      [self.listData insertObject:object atIndex:toRow];    }  
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{    NSInteger from=sourceIndexPath.row;    NSInteger to=destinationIndexPath.row;    id obj=[self.items objectAtIndex:from];    [self.items removeObjectAtIndex:from];    [self.items insertObject:obj atIndex:to];}

2.删除

首先是判断(UITableViewCellEditingStyle)editingStyle

//单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格  -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  {      return UITableViewCellEditingStyleDelete;  }  
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  {      if (editingStyle==UITableViewCellEditingStyleDelete) {  //        获取选中删除行索引值          NSInteger row = [indexPath row];  //        通过获取的索引值删除数组中的值          [self.listData removeObjectAtIndex:row];  //        删除单元格的某一行时,在用动画效果实现删除过程          [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];      }  }  

3.插入

实现方法和删除方法相同,首先还是返回单元格编辑风格

/单元格返回的编辑风格,包括删除 添加 和 默认  和不可编辑三种风格  -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  {      return UITableViewCellEditingStyleInsert;  }  
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  {      if (editingStyle==UITableViewCellEditingStyleDelete) {  //        获取选中删除行索引值          NSInteger row = [indexPath row];  //        通过获取的索引值删除数组中的值          [self.listData removeObjectAtIndex:row];  //        删除单元格的某一行时,在用动画效果实现删除过程          [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];      }      if(editingStyle==UITableViewCellEditingStyleInsert)      {          i=i+1;          NSInteger row = [indexPath row];          NSArray *insertIndexPath = [NSArray arrayWithObjects:indexPath, nil];          NSString *mes = [NSString stringWithFormat:@"添加的第%d行",i];  //        添加单元行的设置的标题          [self.listData insertObject:mes atIndex:row];          [tableView insertRowsAtIndexPaths:insertIndexPath withRowAnimation:UITableViewRowAnimationRight];      }  }  

4.推荐一种已经实现好的可以滑动出现效果的:SWTableViewCell

0 0
原创粉丝点击