首页 > 代码库 > UITableView (4): 在TableView中移动cell和Section 从TableView中删除cell和section

UITableView (4): 在TableView中移动cell和Section 从TableView中删除cell和section

一  .问题:你想用流畅直观的动画来移动和拖拽TableView中的cell和section

方案:

  用moveSection:toSection:方法把一个Section移动到新位置.

  用moveRowAtIndexPath:toIndexPath:方法把一个cell从当前位置移动到新位置

例子:

  创建一个TableView并在其中加载3个Section,每个Section有3个cell

#pragma - mark 初始化数据- (NSMutableArray *)newSectionWithIndex:(NSUInteger)Index withCellCount:(NSUInteger)cellCount{    NSMutableArray *result = [[NSMutableArray alloc]init];    for (NSUInteger counter = 0; counter < cellCount; counter++) {        [result addObject:[[NSString alloc]initWithFormat:@"Section %lu Cell %lu",Index,counter + 1]];    }    return result;}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    //在实例化方法中填充数据    if (self != nil) {        _arrayOfSections = [[NSMutableArray alloc]init];        NSMutableArray *section1 = [self newSectionWithIndex:1 withCellCount:3];        NSMutableArray *section2 = [self newSectionWithIndex:2 withCellCount:3];        NSMutableArray *section3 = [self newSectionWithIndex:3 withCellCount:3];        [_arrayOfSections addObject:section1];        [_arrayOfSections addObject:section2];        [_arrayOfSections addObject:section3];    }    return self;}

以上功能要实现 ,在AppDelegate中实现

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    ViewController *vc = [[ViewController alloc]initWithNibName:nil bundle:nil];    self.window.rootViewController = vc    ;    return YES;}

然后创建TableView并实现相关协议方法

#pragma mark - 协议方法- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return _arrayOfSections.count;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [[_arrayOfSections objectAtIndex:section]count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *result = nil;    static NSString *cellIdentifier = @"CellIdentifier";    result = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];    if (result == nil) {        result = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];    }    result.textLabel.text = [[_arrayOfSections objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];    return result;}
- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    _myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];    _myTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;    _myTableView.dataSource = self;    _myTableView.delegate = self;        [self.view addSubview:_myTableView];        NSTimer *timer = [NSTimer timerWithTimeInterval:2.0f target:self selector:@selector(moveSection1ToSection3) userInfo:nil repeats:NO];    [timer fire];}

最后实现section 和 cell 的相关移动  ,功能实现代码如下:

#pragma mark - Section 和 cell 是如何实现移动的//section1 移动到section3- (void)moveSection1ToSection3{    //数据操作    NSMutableArray *section1 = [_arrayOfSections objectAtIndex:0];    [_arrayOfSections removeObject:section1];    [_arrayOfSections addObject:section1];    //TableView移动section    [_myTableView moveSection:0 toSection:2];        [self moveCellInSection1ToCell2InSection1];}//同section移动cell- (void)moveCellInSection1ToCell2InSection1{    //数据    NSMutableArray *section1 = [_arrayOfSections objectAtIndex:0];    NSString *cell1InSection1 = [section1 objectAtIndex:0];    [section1 removeObject:cell1InSection1];    [section1 insertObject:cell1InSection1 atIndex:1];    //视图    [_myTableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] toIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];        [self moveCell2InSection1ToCell1InSection2];}//不同section移动cell- (void)moveCell2InSection1ToCell1InSection2{    NSMutableArray *section1 = [_arrayOfSections objectAtIndex:0];    NSMutableArray *section2 = [_arrayOfSections objectAtIndex:1];    NSString *cell2InSection1 = [section1 objectAtIndex:1];    [section1 removeObject:cell2InSection1];    [section2 insertObject:cell2InSection1 atIndex:0];        [_myTableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] toIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];}

 

二.从TableView中删除cell和section

1.删除section:

  首先删除数据源中的section;

  然后调用TableView的实例方法deleteSection:withRowAnimation: 第一个参数是NSIndexSet类型,可通过indexSetWithIndex: 来实例化 且indexSetWithIndexesInRange:能

2.删除cell:

  首先删除cell对应的数据;

  deleteRowsAtIndexPaths:withRowAnimation:    

#pragma mark - 删除cell和section//删除section- (void)deleteSection1{    //数据源    [_arrayOfSections removeObjectAtIndex:0];    //然后删除TableView的section    NSIndexSet *sectionToDelete = [NSIndexSet indexSetWithIndex:0];    [_myTableView deleteSections:sectionToDelete withRowAnimation:UITableViewRowAnimationAutomatic];    [self deleteCell2InSection3];}//删除cell- (void)deleteCell2InSection3{    NSMutableArray *section3 = [_arrayOfSections objectAtIndex:[_arrayOfSections count]-1];    [section3 removeObjectAtIndex:1];    //添加多个indexPath删除多行    [_myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:1 inSection:2], nil] withRowAnimation:UITableViewRowAnimationAutomatic];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}

 

UITableView (4): 在TableView中移动cell和Section 从TableView中删除cell和section