更新tableView的某个cell
更新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
核心:
更新tableView的某个cell的更多相关文章
- iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见
iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见 (2015-12-05 12:48:20)[编辑][删除] 转载▼ 首先我们先明确一下问题: 1.因为UI是在主线 ...
- 使用HVTableView动态展开tableView中的cell
使用HVTableView动态展开tableView中的cell 效果: 源码: HVTableView.h 与 HVTableView.m // // HVTableView.h // HRVTab ...
- Swift - 设置tableView每个分区cell圆角
1.// 重新绘制cell边框 func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRow ...
- 优化tableView加载cell与model的过程
优化tableView加载cell与model的过程 效果图 说明 1. 用多态的特性来优化tableView加载cell与model的过程 2. swift写起来果然要比Objective-C简洁了 ...
- 动态切换tableView中的cell的种类
动态切换tableView中的cell的种类 为什么要动态切换tableView中cell的种类呢?如果项目经理不出这种需求,你也就见不到这篇文章了:) 效果: 源码: 首先,你要准备3种cell,直 ...
- ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用
做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头. 在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上 ...
- iOS中UITableView数据源刷新了,但tableview当中的cell没有刷新
你会不会遇到通过断点查看数据源模型的确刷新了,但是tableview没有刷新的情况,我遇到了,并通过下面的方法解决了,供大家参考! 在tableview中的数据源代理方法 p.p1 { margin: ...
- tableview 重用nib cell
#import "ViewController.h" #import "NewsTableViewCell.h" #define UISCREEN_HEIGHT ...
- 在tableView中设置cell的图片和文字
// 设置UITableViewCellEditingStyle的 accessoryType UITableViewCellAccessoryNone, // d ...
随机推荐
- 中小团队快速构建SQL自动审核系统
SQL审核与执行,作为DBA日常工作中相当重要的一环,一直以来我们都是通过人工的方式来处理,效率低且质量没办法保证.为了规范操作,提高效率,我们决定引入目前市面上非常流行的SQL自动审核工具Incep ...
- 解释mysql 语句 ——解释CREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
在我们创建mysql数据库的时候我们经常会用到这句SQL:CREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ ...
- Go 开发
0.参数传递永远是值传递,地址也是一种值 1.go 开发环境的配置 2.import 包的几种形式: 1)_,默认导入一个包时,会将包中内容导入再执行包中的init()方法,有时并不需要某个包,只是想 ...
- java NIO系列教程2
7.FileChannel Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 打开Fi ...
- Object-C语言Block的实现方式
开场白 Block基本概念 中间态转换方法 Block编译后结果分析 Block运行时状态与编译状态对比 开场白 Object-C语言是对C语言的扩展,所以将OC源码进行编译的时候,会将OC源 ...
- [PY3]——logging
logging模块的logger.handler.filter.formatter Logger记录器 提供日志接口,供应用代码使用.logger最长用的操作有两类:配置和发送日志消息.可以通过log ...
- kubernetes continually evict pod when node's inode exhausted
kubernetes等容器技术可以将所有的业务进程运行在公共的资源池中,提高资源利用率,节约成本,但是为避免不同进程之间相互干扰,对底层docker, kubernetes的隔离性就有了更高的要求,k ...
- Android应用博客目录
应用有很多,开个博客都放进来方便查找,也方便修改 1 语言类: 1.1 JAVA 基础语言知识JAVA Collection与Collections,Array与Arrays的区别 JAVA练手--S ...
- Node.js之HTPP URL
几乎每门编程语言都会包括网络这块,Node.js也不例外.今天主要是熟悉下Node.js中HTTP服务.其实HTTP模块是相当低层次的,它不提供路由.cookie.缓存等,像Web开发中不会直接使用, ...
- springboot之约定大约配置
前言 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样 ...