table的编辑.删除

#import "AppDelegate.h"

#import "ViewController.h"

@interface AppDelegate ()


@end

//编辑步骤:

//1.tableView处于编辑状态

//2.cell处于编辑状态

//3.告诉cell,它的编辑样式

//4.提交编辑结果


@implementation AppDelegate

- (void)dealloc

{

    [_window release];

    [super dealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    [_window setBackgroundColor:[UIColor whiteColor]];

    [_window makeKeyAndVisible];

    [_window release];

    

    ViewController *main = [[ViewController alloc] init];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:main];

    [_window setRootViewController:nav];

    [main release];

    [nav release];

    

    

    

    return YES;

}



#import <UIKit/UIKit.h>

//枚举的位置

typedef NS_ENUM(NSInteger, EditType) {

    EditTypeIsEditing,

    EditTypeNormal

};




@interface ViewController : UIViewController

@property(nonatomic,retain) NSMutableArray *tableArray;

@property(nonatomic,retain) UITableView *table;

//设置属性的意义

@property(nonatomic,assign) NSInteger EditState;//0:代表正在被编辑,1:代表为未编辑



@end


#import "ViewController.h"


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>


@end


@implementation ViewController

- (void)dealloc

{

    [_tableArray release];

    [super dealloc];

}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        self.tableArray = [NSMutableArray array];

#warning  plist快速完成初始化

//        //取文件路径

//        NSString *str = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];//文件名加后缀

//        NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:str];//路径

//        NSLog(@"array == %@",array);

//        //array里的元素一个一个取出来放到_tableArray

//        [_tableArray addObjectsFromArray:array];

       

        

        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];

        [dic1 setObject:@"孙海东" forKey:@"name"];

        NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];

        [dic2 setObject:@"孙树海" forKey:@"name"];

        [self.tableArray addObject:dic1];

        [self.tableArray addObject:dic2];

        

        //字符串 数组的内存管理  ???

     }

    return self;

    

}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationController.navigationBar.translucent = NO;

    

    [self createSubviews];

    self.EditState = EditTypeNormal;//默认

    

}

- (void)createSubviews

{

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(0,0,50, 50);

    [button setTitle:@"编辑" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:button];

    self.navigationItem.rightBarButtonItem = right;

    [right release];

    

    

    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];

    [_table setBackgroundColor:[UIColor orangeColor]];

    _table.rowHeight = 100;

    [self.view addSubview:_table];

    [_table release];

    

    

    _table.delegate = self;

    _table.dataSource = self;

    

    

}

//点击编辑 / 未编辑

- (void)buttonAction:(UIButton *)sender

{

    NSLog(@"111");

    //tableView处于被编辑状态

    if (self.EditState == EditTypeNormal) {

        [_table setEditing:YES animated:YES];

        self.EditState = EditTypeIsEditing;

        //sender.title = @"完成";//用的系统的按钮

        [sender setTitle:@"完成" forState:UIControlStateNormal];

    }

    else{

    [_table setEditing:NO animated:YES];

    self.EditState = EditTypeNormal;

    //sender.title = @"编辑";

    [sender setTitle:@"编辑" forState:UIControlStateNormal];

    }

}

#warning 返回值是UIView

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];

    return image;

}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

{

    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2.jpg"]];

    return image;

}

#warning 返回值 大写字母数组

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    NSArray *arr = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z", nil];

    return arr;

}

#warning 设置section的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return 100;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

{

    return 200;

}

#warning 让某个cell处于编辑状态

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

       return YES;

}

#warning  告诉cell被编辑的样式

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

{

    if (indexPath.row == 0) {

        return UITableViewCellEditingStyleDelete;

    }

    else{

    return UITableViewCellEditingStyleInsert;

    }

    //return UITableViewCellEditingStyleNone;

}

#warning 滑动删除 提交编辑结果 编辑状态改变完,提交的时候才走

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

{

    //界面和数据是分开的,要先删除数据,在删除界面

    NSLog(@"555");

    //一旦table的数据源(元素个数,结构等)发生了改变,则必须立刻刷新table的界面,即为每个section返回的行数变了

    //[_tableArray removeObjectAtIndex:indexPath.row];


    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [_tableArray removeObjectAtIndex:indexPath.row];

        //删除数据源(大数组里)的数据//弄清楚你想删的是哪个section里的行

    } else if(editingStyle == UITableViewCellEditingStyleInsert) {

        NSMutableDictionary *dic = [NSMutableDictionary dictionary];

        [dic setObject:@"nihao" forKey:@"name"];

        [_tableArray addObject:dic];

    }

    //不停走重用池,只要数据源改变就要刷新数据

    [tableView reloadData];

 }

#warning canMove

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}

#warning move

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

    //将要移动第几个

    NSDictionary *dic = [[_tableArray objectAtIndex:sourceIndexPath.row] retain];

   

    [_tableArray removeObjectAtIndex:sourceIndexPath.row];

    [_tableArray insertObject:dic atIndex:destinationIndexPath.row];

    [dic release];

    [tableView reloadData];

}



#warning 协议昨天学的 每个section有几行

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

{

    return [_tableArray count];

}

#warning 只要上下滑动,就会走这条,走重用池,加载/回收数据

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

{

    static NSString *cellIdentify = @"cell1";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];

    

    if (!cell) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify] autorelease];//要加autorelease

    }

    [cell setBackgroundColor:[UIColor whiteColor]];

    


    

    NSMutableDictionary *dic = [_tableArray objectAtIndex:indexPath.row];

    cell.textLabel.text = [dic objectForKey:@"name"];

    

    

    return cell;

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值