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

一 、 在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];
    
}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,284评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,232评论 4 61
  • 提起武汉,除了热,我能想起的就是热干面。 热干面是武汉的特色小吃,在湖北乃至全国都十分受欢迎。 我不是湖北人,但热...
    雯铱铱阅读 740评论 5 6
  • 原名《世界》,后改名《旺财的奇幻生活》,由于多次被批以狗影射社会,很不受待见。现更名为暴走的旺财。 正文 旺财刚生...
    祖龙汉阅读 342评论 0 2
  • 拼命工作,原来只是逃避妈妈要离开的事实。
    卓别林来也阅读 146评论 0 0