UI第十天:UITableView 编辑

⼀、tableView编辑
  编辑步骤 :
 1.
开启 TableViwe 编辑状态
 2.
允许那个分区的那行 是可以编辑的 ( 默认是都能编辑 )
 3.
指定可以编辑样式 ( 删除 or 添加 )
 4.
完成编辑
  
完成编辑步骤 :
   1.
操作数据源数组 ( 添加 删除 )
   2.刷新UI界面
-(void)edit:(UIBarButtonItem *)bar
{
   
// 开启编辑状态
    [
self.table setEditing:!self.table.editing animated:YES];
   
//编辑时更改标题
   
if (self.table.editing == YES) {
        bar.
title = @"完成";
    }
else
    {
        bar.
title = @"编辑";
    }
}

// 2.允许编辑
-(
BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
   
//可以控制哪个分区的哪行不能编辑
   
return YES;
}
// 3.指定编辑的样式
-(
UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
NSMutableArray *arr = [NSMutableArray array];
   
if (indexPath.section == 0) {
        arr =
self.array1;
    }
else
    {
        arr =
self.array2;
    }
   
if ([arr[indexPath.row] isEqualToString:@"添加"]) {
       
return UITableViewCellEditingStyleInsert;
    }
   
return UITableViewCellEditingStyleDelete;
}
// 提交编辑
// 根据编辑的样式和索引去完成编辑
-(
void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   
if (indexPath.section == 0) {
       
if(editingStyle == UITableViewCellEditingStyleDelete)
        {
           
//删除
            [
self.array1 removeObjectAtIndex:indexPath.row];
           
//删除刷新
           
// 该方法可以进行多行删除 数组中填写所有已经删除的索引
            [tableView
deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        }
else
        {
           
NSString *str = [NSString stringWithFormat:@"%ld",self.array1.count ];
           
//添加
            [
self.array1 insertObject:str atIndex:indexPath.row];
           
//页面刷新
            [tableView
insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
else
    {
       
if(editingStyle == UITableViewCellEditingStyleDelete)
        {
           
//删除
            [
self.array2 removeObjectAtIndex:indexPath.row];
           
            [tableView
deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
           
        }
else
        {
           
//添加
//             NSString *str = [NSString stringWithFormat:@"%ld",self.array2.count ];
            [
self.array2 insertObject:@"0123"atIndex:indexPath.row];
            [tableView
insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
   
// 刷新页面
   
// 整体刷新(UITableView)重新走一边数据原代理方法达到刷新效果
//    [tableView reloadData];
}
// 删行
if (arr.count == 1) {
        [
self.dataDic removeObjectForKey:self.arr[indexPath.section]];
       
self.arr =[self.dataDic.allKeys sortedArrayUsingSelector:@selector(compare:)];
        [
self.table deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
    }
[NSIndexSet indexSetWithIndex:indexPath.section] //要删第几行
⼆、tableView移动
/*
 1.
开启编辑状态
 2.
允许那个分区移动
 3.
完成移动
  1.
操作数据愿数组
  2.
完成刷新

 */
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
   
return YES;
}
-(
void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
   
//sourceIndexPath 来源的索引
   
//destinationIndexPath 目的地的索引
   
if(sourceIndexPath.section == destinationIndexPath.section)
    {
       
//同区
       
if(sourceIndexPath.section == 0)
        {
           
//把来源索引下数组对应的元素保存一下
           
NSString *str = self.array1[sourceIndexPath.row] ;
           
//用来源的索引删除数组中数据
            [
self.array1 removeObjectAtIndex:sourceIndexPath.row];
           
//插入目的地索引处
            [
self.array1 insertObject:str atIndex:destinationIndexPath.row];
            [tableView
moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
        }
else
        {
           
NSString *str = self.array2[sourceIndexPath.row] ;
            [
self.array2 removeObjectAtIndex:sourceIndexPath.row];
            [
self.array2 insertObject:str atIndex:destinationIndexPath.row];
            [tableView
moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
        }
   
    }
else{
       
//跨区
    }
}
//限制跨区移动
- (
NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
   
//sourceIndexPath 来源索引
   
//proposedDestionaionIndexPath 建议目的地索引
   
if(sourceIndexPath.section == proposedDestinationIndexPath.section)
    {
       
//同区可以返回 目的地索引
       
return proposedDestinationIndexPath;
    }
else
    {
       
//跨区移动需要限制
       
return sourceIndexPath;
    }
}
FoodModel *model = [self.dataArray[fromIndexPath.row] retain];
   
//  删除数据
   
//  我再数组中删除一个对象 相当于给该对象发送一个 release 消息
   
//  如果这个对象 只存在数组中, 那么删除了 就相当于释放了
   
//  我上面定义的指针 就相当于 指向了一个已经被释放的空间
//  retain就要release 使用完后就释放了
    [model release];


三、UITableViewController
UITableViewController继承⾃UIViewController,⾃带⼀个tableView
self.view不是UIView⽽是UITableView
datasource和delegate默认都是self(UITableViewController)
开发中只需要建⽴UITableViewController⼦类
总结
⽆论编辑还是移动,都先让tableView进⼊编辑状态。
编辑结束或者移动结束,要先修改数组或字典中的数据,在更改 UI。
UITableViewController是封装好了各种delegate和datasource,能 提⾼我们开发速度。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值