//
// 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