ios之tableivew beginUpdates() 和 e

2016-12-22  本文已影响0人  FongG

insertRows(at:with:)
https://developer.apple.com/reference/uikit/uitableview/1614879-insertrows

描述:

UITableView calls the relevant delegate and data source methods immediately afterward to get the cells and other content for visible cells.
When this method when is called in an animation block defined by the beginUpdates() and endUpdates() methods,

UITableView defers(延缓) any insertions of rows or sections until after it has handled the deletions of rows or sections. 

This order is followed regardless of how the insertion and deletion method calls are ordered. 

This is unlike inserting or removing an item in a mutable array, in which the operation can affect the array index used for the successive insertion or removal operation.

 For more on this subject, see Batch Insertion, Deletion, and Reloading of Rows and Sections in Table View Programming Guide for iOS.

翻译:

  1. tableview会让可见的cell响应代理方法和数据代理方法

  2. 如果使用了 beginUpdates() 和 endUpdates(),无论在处理块中如何排序,插入数据的方法都会被放在删除数据之后。

  3. 这与在可变数组中插入或移除项目不同,其中操作可影响用于连续插入或移除操作的数组索引。

deleteRows(at:with:)
https://developer.apple.com/reference/uikit/uitableview/1614960-deleterows

描述与insert的第2、3点一样(但实际上deleteRows操作也会影响dateSource中的cellForRow方法,但不走cellForRow)。


</br>

下面做一个例子

//
//  TableViewController.m
//  LableDemo
//
//  Created by 金永峰 on 16/12/22.
//  Copyright © 2016年 金永峰. All rights reserved.
//

#import "TableViewController.h"

@interface TableViewController ()
@property (nonatomic, strong) NSMutableArray *sectionArrys;

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.sectionArrys = [NSMutableArray arrayWithCapacity:0];
    NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"A",@"B",@"C",@"D", nil];
    NSMutableArray *array2 = [NSMutableArray arrayWithObjects:@"a",@"b",@"c",@"d", nil];
    NSMutableArray *array3 = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4", nil];
    [self.sectionArrys addObject:array1];
    [self.sectionArrys addObject:array2];
    [self.sectionArrys addObject:array3];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionArrys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"%s", __func__);
    NSArray *arry = self.sectionArrys[section];
    return arry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:@"cell"];
    }
    
    NSArray *arry = self.sectionArrys[indexPath.section];
    cell.textLabel.text = arry[indexPath.row];
    return cell;
}

#pragma mark - 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    //更新数据源
    //a b  c  d
    
    //b  c  d
    //b  c
    //b  m  c
    //b  m  c  s
    
    //户撒些1 , 3
    
    NSMutableArray *array0 = (NSMutableArray *)self.sectionArrys[0];
    [array0 removeObjectAtIndex:0];
    [array0 removeObjectAtIndex:2];

    [array0 insertObject:@"M" atIndex:1];
    [array0 insertObject:@"S" atIndex:3];
    
    //b d
    //b m d s
//    //只有对插入的操作有刷新数据操作

    //创建相应的indexPaths数组
    NSArray *indexPathsDelete = @[[NSIndexPath indexPathForRow:0 inSection:0],[NSIndexPath indexPathForRow:2 inSection:0]];
    NSArray *indexPathsInsert = @[[NSIndexPath indexPathForRow:1 inSection:0],[NSIndexPath indexPathForRow:3 inSection:0]];

    
    //执行操作
    [self.tableView beginUpdates];

    [self.tableView insertRowsAtIndexPaths:indexPathsInsert withRowAnimation:UITableViewRowAnimationFade];
    
    [self.tableView deleteRowsAtIndexPaths:indexPathsDelete withRowAnimation:UITableViewRowAnimationFade];

    [self.tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}



@end

界面显示

屏幕快照 2016-12-22 下午4.30.24.png

数据显示:

屏幕快照 2016-12-22 下午4.30.12.png

为什么会出现这样的情况呢?

屏幕快照 2016-12-22 下午4.41.46.png

总结:在同时使用tableview的insertRows和deleteRows方法时,需要注意数据的错乱。实在不行可以使用reload刷新数据,但要注意同时不同使用beginUpdates()和endUpdates()方法块。

上一篇下一篇

猜你喜欢

热点阅读