UI_表视图编辑(删除,移动,插入)

#import "AppDelegate.h"

#import "RootViewController.h"

#import "TableViewController.h"

#import "TwoTableViewController.h"

#import "TestTableViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    

//    RootViewController *rootVC = [RootViewController new];

//    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:rootVC];

//    self.window.rootViewController = navC;

   

    

//    TableViewController *tableVC = [[TableViewController alloc]init];

//    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:tableVC];

//    self.window.rootViewController = navC;

    

    

    TwoTableViewController *twoVC = [TwoTableViewController new];    

    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:twoVC];

    self.window.rootViewController = navC;

    

    

//    TestTableViewController *testVC = [TestTableViewController new];

//    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:testVC];

//    self.window.rootViewController = navC;

    

    

    

    

    

    

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}








#import "RootViewController.h"


@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>


@property(nonatomic,retain)NSMutableArray *allDataMutableArray;  //存储数据

@end


@implementation RootViewController

//懒加载

-(NSMutableArray *)allDataMutableArray{

    if (!_allDataMutableArray) {

        _allDataMutableArray = [NSMutableArray new];

    }

    return _allDataMutableArray;

}


//造假数据

-(void)createData{

    [self.allDataMutableArray addObject:@"HanOBa"];

    [self.allDataMutableArray addObject:@"Hello"];

    [self.allDataMutableArray addObject:@"World"];

    [self.allDataMutableArray addObject:@"添加一行"];

}





- (void)viewDidLoad {

    [super viewDidLoad];

//表视图编辑

    self.navigationItem.title = @"表视图编辑";

    

//新建一个表视图和屏幕大小一致

    UITableView *myTableView = [[UITableView alloc]initWithFrame:self.view.bounds];

    myTableView.tag = 1000;

//指定代理并实现必须实现的代理方法

    myTableView.delegate = self;

    myTableView.dataSource = self;

    

//将表视图添加到父视图上

    [self.view addSubview:myTableView];

    

//注册单元格

    [myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];

    

//调用假数据

    [self createData];

    

//系统提供的一个编辑按钮(右按钮--删除)

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    

//自己编辑一个左按钮(左按钮--刷新)

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(leftAction)];

}


//实现左按钮的方法:

-(void)leftAction{

    UITableView *tableView = (UITableView *)[self.view viewWithTag:1000];

    //刷新tableView上面的数据,实际上就是让实现的代理方法重新执行一次

    [tableView reloadData];

}











#pragma mark - 必须实现的方法!!!  分区的行数和分区数不能给死,要给数据值,变量!!!

//定义单元格

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //从重用队列中获取一个cell,注册单元格时候给的重用标示符是什么,这里的重用标示符就是什么!!!

    UITableViewCell *mycell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];

    mycell.textLabel.text = self.allDataMutableArray[indexPath.row];

    

    return mycell;

}

//每个分区的行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    

    return self.allDataMutableArray.count;

}




#pragma mark - 编辑、修改等的代理方法

//系统提供的开启当前视图控制器的编辑模式

-(void)setEditing:(BOOL)editing animated:(BOOL)animated{


    [super setEditing:editing animated:animated];

    

    //将表示图的编辑状态开启

    UITableView *myTableView = (UITableView *)[self.view viewWithTag:1000];

    [myTableView setEditing:editing animated:animated];

    

}

//设置编辑样式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{


//编辑,插入

    //return UITableViewCellEditingStyleInsert;

//删除

    //return UITableViewCellEditingStyleDelete;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值