[11]UITableView编辑 删除 添加 移动 单例 及其去掉TableView没内容的Cell

UITableView的编辑 cell的添加,删除.

接UITableView[1]我们对之前的视图代码进行编辑

视图编辑四步走

1.设置表示图进入可编辑状态

2.指定哪些可以被编辑

3.设置表示图编辑样式是删除还是添加 默认是删除

4.//编辑我弄成之后需要做什么事情,事件的处理

#pragma mark  -------表视图编辑--------
//将表示图处于可编辑状态
- (void)setEdting:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:edting animated:animated];
    [_tableView setEdting:editng animated:animated];
}
//指定表视图哪些行可以进行编辑 默认是打开的
- (void)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//设置表示图编辑样式是删除还是添加 默认是删除
- (UITableViewCellEditingStyle)tableView:(UITableView                                    *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  UITableViewCellEditingStyleInsert;
//    UITableViewCellEditingStyleNone,
//    UITableViewCellEditingStyleDelete,
//    UITableViewCellEditingStyleInsert
}
//编辑我弄成之后需要做什么事情,这才是我们编辑过程的核心代码,项目开发中,增加,删除的都在这里完成的
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:
(NSIndexPath *)indexPath
{
    
    [tableView beginUpdates];
//    NSLog(@"你删不掉的");
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //第一步 将选中的单元格删除            //[indexpath]代表的这行的数据 是个数组
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        //第二步 将删除的单元格之前现实的内容从数组中移除  如果没有 updates必须在第一删数据
        [_studentArray removeObjectAtIndex:indexPath.row];
    }
    else{
        NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:0 inSection:0];
        [tableView insertRowsAtIndexPaths:@[indexPath1] withRowAnimation:UITableViewRowAnimationLeft];
        
        Student *stu = [[Student alloc] init];
        stu.name = @"张三";
        stu.gender = @"男";
        stu.age = @"22";
        stu.picture = @"0";
        
        [_studentArray insertObject:stu atIndex:0];
        [stu release];
    }
    [tableView endUpdates];
} 

 

表示图移动 三步骤


#pragma mark  -------表示图编辑---------
//第一步同上 开启编辑模式 
//第二步 可移动 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:
(NSIndexPath *)indexPath
{
    return YES;
}
//移动完需要做什么事,这才是移动的核心方法,之前的都是辅助的.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:
(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath
                                            *)destinationIndexPath
{
    Student *stu = [_studentArray objectAtIndex:sourceIndexPath.row];
    [_studentArray removeObjectAtIndex:sourceIndexPath.row];
    [_studentArray insertObject:stu atIndex:destinationIndexPath.row];

    //交换位置
//    [_studentArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];

}

UITableViewController

UITableViewController继承自UIViewController,自带一个tableView
self.view 不是UIView而是UITableView
datasource和delegate默认都是self(UITableViewController)
开发中主要建立UITableViewController子类

单例设计模式

顾名思义:一个类只有唯一的一个实例
系统的单例类有UIDevice,UIScreen,NSBundle,NSFileManager,NSUserDefaults等等.
单例类一般会提供一个类方法,获得这个唯一的实例

总结:无论编辑还是移动,都先让tableView进⼊入编辑状态。
编辑结束或者移动结束,要先修改数组和字典中的数据,在更改UI
UITableViewController是封装好了各种delegate和datasource,能 提⾼高我们开发速度。
这个类方法常以default,share,main等单词开头,与便利构造器的命名规范是不同的,
我们自己在定义单例类的时候,一定要保证外界能获得唯一的实例! 即每次获得同一个对象.
一段新建了个Singleton类 继承NSObject

//如何保证这个单例获取的是同一个对象
+ (Singleton *)shareInstance
{
    //同步锁 解决多线程的情况时候出现问题,只有先创建一个才会创建另一个
    //如果是在多线程里面,刚开始的时候,如果有俩个线程同时第一次调用这个方法,会产生俩个线程创建了俩个对象,为了避免这种情况的发生,我们需要做如下修改:
    @synchronized(self){ //添加了一个同步锁,同步锁的作用就是一次只能有一个线程在访问{}内部的内容,如果有多个内容的话,那么其他线程就处于等待状态.
    if(s == nil){
        s = [[Singleton alloc] init];
//        s = [[super allocWithZone:nil] init];
    }
    }
    return s;
}

小技巧

TableView不显示没内容的Cell怎么办?,下面一段代码搞定,用过的都说好

self.tableView.tableFooterView = [[UIView alloc] init];
On the road。。。
原文地址:https://www.cnblogs.com/ianhao/p/4477914.html