更新tableView的某個cell

更新tableView的某個cell異步

異步加載完數據後更新某個cell,這應該是很是常見的使用方法了,咱們常常會用reloadData.動畫

效果:atom

源碼:spa

//
//  RootViewController.m
//  DataTableView
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "SDWebImage.h"

@interface RootViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView  *showTableView;
@property (nonatomic, strong) NSArray      *dataArray;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 初始化tableView
    _showTableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                                  style:UITableViewStylePlain];
    _showTableView.delegate   = self;
    _showTableView.dataSource = self;
    [self.view addSubview:_showTableView];
    
    
    // 下載數據源
    NSString *oneImageURL = @"http://pic.cnitblog.com/avatar/607542/20140226182241.png";
    [[SDWebImageManager sharedManager]
     downloadWithURL:[NSURL URLWithString:oneImageURL]
     options:0
     progress:^(NSInteger receivedSize, NSInteger expectedSize)
     {
         
     }
     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
     {
         _dataArray = @[image];
         
         // 更新某個cell的數據
         [_showTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]
                               withRowAnimation:UITableViewRowAnimationFade];
     }];
}

#pragma mark - UITableView delegate & dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reusedID = @"YouXianMing";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:reusedID];
    }
    
    // 第一個cell
    if (indexPath.row == 0)
    {
        if ([_dataArray count] > 0)
        {
            cell.imageView.image = _dataArray[0];
        }
    }
    
    // 第二個cell
    if (indexPath.row == 1)
    {
        if ([_dataArray count] > 0)
        {
            cell.imageView.image = _dataArray[0];
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

@end

核心:3d

最主要的是,reloadRowsAtIndexPaths能有動畫效果.code

 

附錄:blog

從新給了高度值也是有動畫的哦:)源碼

源碼:it

//
//  RootViewController.m
//  DataTableView
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "SDWebImage.h"

@interface RootViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView  *showTableView;
@property (nonatomic, strong) NSArray      *dataArray;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 初始化tableView
    _showTableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                                  style:UITableViewStylePlain];
    _showTableView.delegate   = self;
    _showTableView.dataSource = self;
    [self.view addSubview:_showTableView];
    
    
    // 下載數據源
    NSString *oneImageURL = @"http://wallpapers.wallbase.cc/rozne/wallpaper-573934.jpg";
    [[SDWebImageManager sharedManager]
     downloadWithURL:[NSURL URLWithString:oneImageURL]
     options:0
     progress:^(NSInteger receivedSize, NSInteger expectedSize)
     {
         NSLog(@"%f", (float)receivedSize/(float)expectedSize);
     }
     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
     {
         _dataArray = @[image];
         
         // 更新某個cell的數據
         [_showTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]
                               withRowAnimation:UITableViewRowAnimationFade];
     }];
}

#pragma mark - UITableView delegate & dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reusedID = @"YouXianMing";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:reusedID];
    }
    
    // 第一個cell
    if (indexPath.row == 0)
    {
        if ([_dataArray count] > 0)
        {
            cell.imageView.image = _dataArray[0];
        }
    }
    
    // 第二個cell
    if (indexPath.row == 1)
    {
        if ([_dataArray count] > 0)
        {
            cell.imageView.image = _dataArray[0];
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 加載完數據後動態展開
    if (indexPath.row == 0)
    {
        if ([_dataArray count] > 0)
        {
            return 200;
        }
    }
    
    return 70;
}

@end

核心:io

相關文章
相關標籤/搜索