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

  • 相关阅读:
    暑假集训(2)第九弹 ----- Points on Cycle(hdu1700)
    暑假集训(1)第八弹 -----简单迷宫(Poj3984)
    暑假集训(1)第七弹 -----Oil Deposits(Poj1562)
    暑假集训(1)第六弹 -----简单计算器(Hdoj1237)
    暑假集训(1)第五弹 -----Rails(Uva514)
    暑假集训(1)第四弹 -----Find a way(Hdu2612)
    暑假集训(1)第三弹 -----Dungeon Master(Poj2251)
    暑假集训(1)第二弹 -----Catch the cow(Poj3278)
    EF框架搭建(一)
    领域驱动有感<上>
  • 原文地址:https://www.cnblogs.com/zkzzkz/p/5095456.html
Copyright © 2011-2022 走看看