UITableView局部刷新

只刷新cell不刷新section,这问题还难住了一阵子

需要用到:

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

注意:(NSArray *)indexPaths里面的下标要不同的哦,要是往最后插入,要记得看仔细哦

 参考:http://blog.csdn.net/likendsl/article/details/17122505

//
//  ViewController.m
//  tableView
//
//  Created by apple on 15/8/6.
//  Copyright (c) 2015年 tqh. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> {
    UITableView *tableview;
}

@property (nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation ViewController

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc]initWithArray:@[@"231",@"321",@"213"]];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    tableview = [[UITableView alloc]initWithFrame:CGRectMake(00,300600)];
    tableview.dataSource = self;
    tableview.delegate = self;
    [tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
    [self.view addSubview:tableview];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    cell.textLabel.text = @"213";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableview beginUpdates];
    NSArray *array1 = [[NSArray alloc]initWithArray:self.dataSource];
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (int i = 0; i < array1.count; i ++) {
        NSIndexPath *indexPath_1=[NSIndexPath indexPathForRow:i inSection:0];
        [array addObject:indexPath_1];
    }
    array1 = [[NSArray alloc]init];
    [tableview beginUpdates];
    [tableview deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationLeft];
    [tableview endUpdates];
    
    //加载新数据
    self.dataSource = [[NSMutableArray alloc]initWithArray:@[@"",@"",@"",@"",@""]];
    NSArray *array2 = [[NSArray alloc]initWithArray:self.dataSource];
    NSMutableArray *array3 = [[NSMutableArray alloc]init];
    for (int i = 0; i < array2.count; i ++) {
        NSIndexPath *indexPath_1=[NSIndexPath indexPathForRow:i inSection:0];
        [array3 addObject:indexPath_1];
    }
    array2 = [[NSArray alloc]init];
    [tableview insertRowsAtIndexPaths:array3 withRowAnimation:UITableViewRowAnimationRight];
    [tableview endUpdates];
}


@end

 数据从3个变成5个了还带动画效果~~~~~~~

原文地址:https://www.cnblogs.com/hxwj/p/4708867.html