美文网首页
UITableView 编辑

UITableView 编辑

作者: 雷仔 | 来源:发表于2016-02-24 01:06 被阅读68次

- (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_UI_11_UITableView的编辑

    第十一章 UITableView的编辑 一、UITableView编辑 二、UITableViewController

  • iOS 之UITableView编辑

    一. UITableView编辑 UITableView编辑步骤:a.让tableView处于编辑状态 b.确定哪...

  • UITableView--iOS笔记摘录

    UITableView 概念 UITableView的常用方法 UITableView代理方法 常用 选中 编辑(...

  • UITableView 编辑模式详解

    UITableView 编辑模式详解 UITableView的相关编辑操作非常全,今天我们来做一个总结。跟编辑相关...

  • 编辑 UITableView

    对 UITableView 的常见操作包括增加行、删除行和移动行等操作,效果图如下: NSBundle 指定 XI...

  • UITableView 编辑

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NS...

  • 编辑UITableView

    UITableView有一个名为editing的属性,如果将editing属性设置为YES,UITableView...

  • UITableView编辑模式详解

    转载:UITableView 编辑模式详解作者:秋刀生鱼片 UITableView的相关编辑操作非常全,今天我们来...

  • UITableView 编辑模式

    UITableView 编辑模式 相关方法 UITableView 插入、删除和移动表格的行和分区 (Insert...

  • cell左滑删除等操作

    //让tableView可编辑 - (BOOL)tableView:(UITableView*)tableView...

网友评论

      本文标题:UITableView 编辑

      本文链接:https://www.haomeiwen.com/subject/xufikttx.html