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