OC 表格视图的侧滑删除与添加

2017-08-21  本文已影响0人  J_mine

一 、 在AppDelegate.h中设置导航栏

  1. 导入头文件 ViewController.h
  #import "ViewController.h"

2

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 导航控制器
    ViewController *vc = [[ViewController alloc] init];
    UINavigationController *theNav = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = theNav;
    return YES;
}

二、首先 我们要用到表格的两个协议方法以及一个弹出框的协议方法

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

三、 创建表格与数组的全局变量,方便下面的调用

UITableView *theTable;
 NSMutableArray *array;

四 、在viewDidLoad中 对表格进行初始化,并设置的代理, 为其设置两个按钮 ,以及将 数组进行初始化并赋值

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 初始化表格
    theTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    // 代理
    theTable.delegate = self;
    theTable.dataSource = self;
    
    [self.view addSubview:theTable];
    
    // 右按钮
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(right)];
    self.navigationItem.rightBarButtonItem = right;
    // 左按钮
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(left)];
    self.navigationItem.leftBarButtonItem = left;
    
    array = [[NSMutableArray alloc] initWithObjects:@"小李",@"小王",@"小红", nil];
    
}

对两个按钮的方法 进行设置

// 编辑
-(void)left
{
    [theTable setEditing:!theTable.editing animated:YES];
}
// 添加
-(void)right
{
    UIAlertView * alert =[[UIAlertView alloc]initWithTitle:@"信息" message:@"添加" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    alert.alertViewStyle=UIAlertViewStylePlainTextInput;
    
    alert.delegate=self;
    
    [alert show];
    
    
}

弹出框的设置

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        UITextField *field = [alertView textFieldAtIndex:0];
        
        [array addObject:field.text];
        
        [theTable reloadData];
    }
}

表格的协议方法

#pragma mark - 表格方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
    }
    
    cell.textLabel.text = array[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    [array removeObjectAtIndex:indexPath.row];
     [theTable reloadData];
    
}

此代码实现了 表格的左滑删除功能以及添加数据的功能

上一篇 下一篇

猜你喜欢

热点阅读