更新tableView的某个cell

异步加载完数据后更新某个cell,这应该是非常常见的使用方法了,我们经常会用reloadData.

效果:

源码:

//
// 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:
progress:^(NSInteger receivedSize, NSInteger expectedSize)
{ }
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{
_dataArray = @[image]; // 更新某个cell的数据
[_showTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow: inSection:]]
withRowAnimation:UITableViewRowAnimationFade];
}];
} #pragma mark - UITableView delegate & dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ;
} - (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 == )
{
if ([_dataArray count] > )
{
cell.imageView.image = _dataArray[];
}
} // 第二个cell
if (indexPath.row == )
{
if ([_dataArray count] > )
{
cell.imageView.image = _dataArray[];
}
} return cell;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
} @end

核心:

最主要的是,reloadRowsAtIndexPaths能有动画效果.

附录:

重新给了高度值也是有动画的哦:)

源码:

//
// 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:
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: inSection:]]
withRowAnimation:UITableViewRowAnimationFade];
}];
} #pragma mark - UITableView delegate & dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ;
} - (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 == )
{
if ([_dataArray count] > )
{
cell.imageView.image = _dataArray[];
}
} // 第二个cell
if (indexPath.row == )
{
if ([_dataArray count] > )
{
cell.imageView.image = _dataArray[];
}
} return cell;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 加载完数据后动态展开
if (indexPath.row == )
{
if ([_dataArray count] > )
{
return ;
}
} return ;
} @end

核心:

最新文章

  1. tomcat的安全配置(禁用http方法,部署多个应用,启用从安全cookie,指定错误页面和显示信息)
  2. Flags-Ural1225简单递推
  3. jdb调试命令
  4. vs快捷方式
  5. java发送邮件
  6. 二模 (15)day2
  7. JMeter一个错误the target server failed to respond--JMeter坑
  8. GD库知识点
  9. 201521123056 《Java程序设计》第3周学习总结
  10. Oracle常用语句语法汇总
  11. Python库的安装
  12. .Net Core应用框架Util介绍(二)
  13. 报错:Sqoop2 Exception: java.lang.NoSuchMethodError Message: org.apache.hadoop.security.authentication.client.Authenticator
  14. Tomcat集成Memcached Session Manager方案
  15. Array 转 Set
  16. vlookup函数应用
  17. idea自动生成serialVersionUID(转)
  18. 标签流 VS 脚本流
  19. 自己动手写Vue插件Toast
  20. eclipse中如何添加tomcat插件

热门文章

  1. Linux下安装了Xampp,但命令行使用不了MySQL
  2. AngularJS 的常用特性(五)
  3. 玩转mongodb(五):mongodb 3.0+ 查询性能分析
  4. docker搭建私有registry
  5. bzoj 5341: [Ctsc2018]暴力写挂
  6. 线程8--GCD常见用法
  7. MyBatis和Hibernate的优缺点对比。
  8. dubbo的重试机制
  9. 高并发第十二弹:并发容器J.U.C -- Executor组件FutureTask、ForkJoin
  10. SSH下shiro的基本使用