UITableView 编辑

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

{

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

if (self) {

}

return self;

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

NSArray *array1 = [NSArray arrayWithObjects:@"美国", @"法国", nil];

NSArray *array2 = [NSArray arrayWithObjects:@"中国", @"韩国", nil];

NSArray *array3 = [NSArray arrayWithObjects:@"日本", @"朝鲜", @"阿联酋", nil];

self.arr = [NSMutableArray arrayWithObjects:array1, array2, array3, nil];

self.TBV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height - 14) style:UITableViewStyleGrouped];

self.TBV.delegate = self;

self.TBV.dataSource = self;

[self.view addSubview:self.TBV];

[self.TBV release];

//设置一个右边的编辑按钮(系统已经初始化好的)

self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{

[super setEditing:editing animated:animated];

[self.TBV setEditing:editing animated:animated];

}

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

{

return YES;

}

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

{

if (indexPath.section == 2) {

return UITableViewCellEditingStyleInsert;

}

return UITableViewCellEditingStyleDelete;

}

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!编辑的实现

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

{

if (editingStyle == UITableViewCellEditingStyleDelete) {

//数据操作

NSMutableArray *array = [NSMutableArray arrayWithObject:[_arr  objectAtIndex:indexPath.section]];

//数据删除

[self.arr removeObjectAtIndex:indexPath.row];

[_arr replaceObjectAtIndex:indexPath.section withObject:array];

//cell 的操作

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];

}

if (editingStyle == UITableViewCellEditingStyleInsert) {

}

}

////数据操作

//NSMutableArray *array = [NSMutableArray arrayWithObject:[_arr  objectAtIndex:indexPath.section]];

////数据删除

//[self.arr removeObjectAtIndex:indexPath.row];

//[_arr replaceObjectAtIndex:indexPath.section withObject:array];

//

////cell 的操作

//[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];

//}

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

//{

//    NSLog(@"编辑开始");

//    // 先判断当前的编辑模式

//    if (editingStyle == UITableViewCellEditingStyleDelete) {

//        // 先把数组里的对象删除掉

//        [self.arr removeObjectAtIndex:indexPath.row];

//        // 第一种方式

//        //        [self.tableView reloadData];

//        // 第二种方式

//        [self.TBV deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

//    }

//}

//有几个section

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return [_arr count];

}

//每个section上有几行

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

{

return [[_arr objectAtIndex:section] count];

}

//每个section上的内容

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

//重用标识

static NSString *reuse = @"reuse";

//从重用队列里面取

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];

//若没有,初始化

if (!cell) {

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

cell.textLabel.text = [[_arr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

}

return cell;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,948评论 3 38
  • 1、tableView 的编辑模式 进入编辑模式 代码体现 // 设置 editing 属性tableView?....
    早起的虫儿子被鸟吃阅读 11,273评论 0 4
  • 代码下载地址一代码下载地址二 一、总述 在iOS开发中,UITableView的使用率可以说是非常高的,所以它在i...
    酒茶白开水阅读 9,546评论 0 23
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 5,309评论 9 5
  • .通路设计(顾客动线设计) 这是卖场布局的首要元素。动线设计的核心:如何让顾客的行动路线越长越好,让顾客在场内逗留...
    heyelushui520阅读 3,081评论 0 1