UI TableView 编辑

appdelegate.m

 

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    RootViewController * root =[[RootViewController alloc]init];
    UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:root];
    
    nav.navigationBar.barTintColor=[UIColor greenColor];
    nav.navigationBar.translucent=NO;
    
    self.window.rootViewController=nav;
    
    
    
    
    
    return YES;
}

 

 

rootView.m

#import "RootViewController.h"

@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray * dataArray;
    UITableView *tab;
    
}
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    tab =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667-64) style:UITableViewStylePlain];
    
    [self.view addSubview:tab];
    tab.delegate=self;
    tab.dataSource=self;
    
    dataArray =[NSMutableArray array];
    [dataArray addObject:@"宋江"];
    [dataArray addObject:@"武松"];
    [dataArray addObject:@"王英"];
    [dataArray addObject:@"孙行者"];
    
    //系统编辑按钮
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
   
}

//点击编辑按钮执行的方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    // 开启tab编辑
    [tab setEditing:editing animated:animated];


}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.创建静态标识符
    static NSString *identifier =@"cell";
    //2.根据标识符从重用池中取cell
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil){
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        
    }
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    return cell;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
    
}

//cell gaodu
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
    
}
//控制编辑代理方法
//是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(indexPath.row==3){
        return NO;
        
    }
    return YES;
    
}
//允许移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
    
}

//插入还是删除
//-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return UITableViewCellEditingStyleInsert;
//
//
//}

//删除按钮文字
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return @"怼我?";
}


//控制删除和插入操作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle ==UITableViewCellEditingStyleDelete){
        //这里写删除代码
        //1.先删除数组的数据
        [dataArray removeObjectAtIndex:indexPath.row];
        //2.删除cell
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
        
    }

}

//控制移动的方法
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //移动的代码写在这
    //1.获取要移动的数据
    NSString *str =[dataArray objectAtIndex:sourceIndexPath.row];
    //2.删除这一行
    [dataArray removeObjectAtIndex:sourceIndexPath.row];
    //3.插入到新的位置
    [dataArray insertObject:str atIndex:destinationIndexPath.row];
    
}
//点击cell执行方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"你点了第%ld行",indexPath.row+1);
    //刷新列表
    [tab reloadData];
}



@end

 

posted @ 2020-09-03 23:33  逆欢  阅读(154)  评论(0编辑  收藏  举报