table的编辑.删除

来源:互联网 发布:初学者吉他品牌知乎 编辑:程序博客网 时间:2024/06/06 01:53

#import "AppDelegate.h"

#import "ViewController.h"

@interface AppDelegate ()


@end

//编辑步骤:

//1.tableView处于编辑状态

//2.cell处于编辑状态

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

//4.提交编辑结果


@implementation AppDelegate

- (void)dealloc

{

    [_windowrelease];

    [superdealloc];

}


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

   

    self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    [_windowsetBackgroundColor:[UIColorwhiteColor]];

    [_windowmakeKeyAndVisible];

    [_windowrelease];

    

    ViewController *main = [[ViewControlleralloc] init];

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

    [_windowsetRootViewController:nav];

    [mainrelease];

    [navrelease];

    

    

    

    return YES;

}



#import <UIKit/UIKit.h>

//枚举的位置

typedefNS_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];

    [superdealloc];

}

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

{

   self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {

       self.tableArray = [NSMutableArrayarray];

#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 = [NSMutableDictionarydictionary];

        [dic1setObject:@"孙海东"forKey:@"name"];

        NSMutableDictionary *dic2 = [NSMutableDictionarydictionary];

        [dic2setObject:@"孙树海"forKey:@"name"];

        [self.tableArrayaddObject:dic1];

        [self.tableArrayaddObject:dic2];

        

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

     }

    return self;

    

}

- (void)viewDidLoad {

    [superviewDidLoad];

    self.navigationController.navigationBar.translucent = NO;

    

    [selfcreateSubviews];

    self.EditState =EditTypeNormal;//默认

    

}

- (void)createSubviews

{

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];

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

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

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

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

    self.navigationItem.rightBarButtonItem = right;

    [rightrelease];

    

    

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

    [_tablesetBackgroundColor:[UIColororangeColor]];

   _table.rowHeight =100;

    [self.viewaddSubview:_table];

    [_tablerelease];

    

    

   _table.delegate =self;

    _table.dataSource =self;

    

    

}

//点击编辑 / 未编辑

- (void)buttonAction:(UIButton *)sender

{

   NSLog(@"111");

    //tableView处于被编辑状态

   if (self.EditState ==EditTypeNormal) {

        [_tablesetEditing:YESanimated:YES];

        self.EditState =EditTypeIsEditing;

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

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

    }

   else{

    [_tablesetEditing:NOanimated:YES];

    self.EditState =EditTypeNormal;

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

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

    }

}

#warning 返回值是UIView

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

{

    UIImageView *image = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"1.jpg"]];

   return image;

}

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

{

    UIImageView *image = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"2.jpg"]];

   return image;

}

#warning 返回值 大写字母数组

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

{

    NSArray *arr = [NSArrayarrayWithObjects:@"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) {

        returnUITableViewCellEditingStyleDelete;

    }

   else{

    returnUITableViewCellEditingStyleInsert;

    }

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

        [_tableArrayremoveObjectAtIndex:indexPath.row];

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

    } elseif(editingStyle ==UITableViewCellEditingStyleInsert) {

        NSMutableDictionary *dic = [NSMutableDictionarydictionary];

        [dicsetObject:@"nihao"forKey:@"name"];

        [_tableArrayaddObject:dic];

    }

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

    [tableViewreloadData];

 }

#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 = [[_tableArrayobjectAtIndex:sourceIndexPath.row]retain];

   

    [_tableArrayremoveObjectAtIndex:sourceIndexPath.row];

    [_tableArrayinsertObject:dic atIndex:destinationIndexPath.row];

    [dicrelease];

    [tableViewreloadData];

}



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

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

{

    return [_tableArraycount];

}

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

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

{

   static NSString *cellIdentify =@"cell1";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];

    

   if (!cell) {

        cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellIdentify] autorelease];//要加autorelease

    }

    [cell setBackgroundColor:[UIColorwhiteColor]];

    


    

   NSMutableDictionary *dic = [_tableArrayobjectAtIndex:indexPath.row];

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

    

    

   return cell;

}




0 0
原创粉丝点击