zoukankan      html  css  js  c++  java
  • tableView自带删除与添加操作

    //

    //  ViewController.m

    //  tableView自带删除与添加操作

    //

    //  Created by 张凯泽 on 16/1/2.

    //  Copyright © 2016年 rytong_zkz. All rights reserved.

    //

    #import "ViewController.h"

    #define ZKZAddOrDeleteValue AddValue

    @interface ViewController ()

    @property(nonatomic,strong)NSMutableArray *array;

    @end

    @implementation ViewController

    //-(BOOL)prefersStatusBarHidden

    //{

    //    return YES;

    //}

    -(NSMutableArray*)array

    {

        if (_array == nil) {

            _array = [NSMutableArray array];

            for (int i = 0; i<20; i++) {

                [_array addObject:@(i)];

            }

        }

        return _array;

    }

    -(void)viewDidLoad

    {

        //隐藏状态栏

        [UIApplication sharedApplication].statusBarHidden = YES;

        //1.进行上一步操作的时候要在info.plist中View controller-based status bar appearance设置为NO

        UIBarButtonItem * bRash = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(rashClick)];

        UIBarButtonItem * bAdd = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(AddClick)];

        NSArray * aa = [NSArray arrayWithObjects:bRash,bAdd, nil];

        self.navigationItem.rightBarButtonItems = aa;

    }

    -(void)rashClick

    {

        //设置可编辑模式

        [self.tableView setEditing:!self.tableView.editing animated:YES];

    }

    -(void)AddClick

    {

        //设置可编辑模式

        [self.tableView setEditing:!self.tableView.editing animated:YES];

    }

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {

        return 1;

    }

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

    {

        return self.array.count;

    }

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

    {

        static NSString * ID = @"cell";

        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];

        if (cell == nil) {

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

        }

        NSNumber * number = [self.array objectAtIndex:indexPath.row];

        cell.textLabel.text =[NSString stringWithFormat:@"ios=%@",number] ;

        return cell;

    }

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

    {

        if (editingStyle == UITableViewCellEditingStyleDelete) {

            //删除模型数据

            [self.array removeObjectAtIndex:indexPath.row];

            //进行刷新

            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

        }else if (editingStyle == UITableViewCellEditingStyleInsert )

        {

            [self.array insertObject:@(111023) atIndex:indexPath.row];

            [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

        }

    }

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

    {

    #ifdef ZKZAddOrDeleteValue

        if (indexPath.row %2 == 0 ) {

            return UITableViewCellEditingStyleInsert;

            

        }

    #endif

        return UITableViewCellEditingStyleDelete;

    }

    @end

  • 相关阅读:
    js,vue.js一些方法的总结
    confirm提示弹出确定和取消按钮
    移动端 meta 必备
    Vue.js总结 [2017.6.5]
    2017.6.5项目总结(移动端touch事件)
    微信公众平台接口开发(全面认识接口)
    数据库作业
    数据库子函数等
    判断一年是否为闰年
    数据库练习
  • 原文地址:https://www.cnblogs.com/zkzzkz/p/5095456.html
Copyright © 2011-2022 走看看