更新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的更多相关文章

  1. iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见

    iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见 (2015-12-05 12:48:20)[编辑][删除] 转载▼     首先我们先明确一下问题: 1.因为UI是在主线 ...

  2. 使用HVTableView动态展开tableView中的cell

    使用HVTableView动态展开tableView中的cell 效果: 源码: HVTableView.h 与 HVTableView.m // // HVTableView.h // HRVTab ...

  3. Swift - 设置tableView每个分区cell圆角

    1.// 重新绘制cell边框 func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRow ...

  4. 优化tableView加载cell与model的过程

    优化tableView加载cell与model的过程 效果图 说明 1. 用多态的特性来优化tableView加载cell与model的过程 2. swift写起来果然要比Objective-C简洁了 ...

  5. 动态切换tableView中的cell的种类

    动态切换tableView中的cell的种类 为什么要动态切换tableView中cell的种类呢?如果项目经理不出这种需求,你也就见不到这篇文章了:) 效果: 源码: 首先,你要准备3种cell,直 ...

  6. ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用

    做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头. 在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上 ...

  7. iOS中UITableView数据源刷新了,但tableview当中的cell没有刷新

    你会不会遇到通过断点查看数据源模型的确刷新了,但是tableview没有刷新的情况,我遇到了,并通过下面的方法解决了,供大家参考! 在tableview中的数据源代理方法 p.p1 { margin: ...

  8. tableview 重用nib cell

    #import "ViewController.h" #import "NewsTableViewCell.h" #define UISCREEN_HEIGHT ...

  9. 在tableView中设置cell的图片和文字

    // 设置UITableViewCellEditingStyle的 accessoryType UITableViewCellAccessoryNone,                   // d ...

随机推荐

  1. webpack局部安装的问题

    webpack的局部安装 npm install webpack 默认状态是当前目录下安装,-g是全局安装 ---------------------------------------------- ...

  2. tomcat启动(三)Catalina分析-load方法分析

    load()方法按从上到下顺序分析(主要分析本人所没学过的知识点,其它略过...). Digester类作用 使用sax技术对xml进行解析 未开始解析时Digester.push(this)这个用来 ...

  3. centos 关闭selinux 临时关闭selinux 报错 setenforce: setenforce() failed

    关闭selinux的方法有两种:临时关闭和永久关闭. 查看selinux的状态:estatus [root@--- ~]# sestatus SELinux status: enabled SELin ...

  4. koa2开发入门

    一.koa2入门 1.创建koa2工程 首先,我们创建一个目录hello-koa并作为工程目录用VS Code打开.然后,我们创建app.js,输入以下代码: // 导入koa,和koa 1.x不同, ...

  5. [Linux]——进程管理相关

    一些概念 程序program:通常以二进制程序放置在存储媒介中,以物理文件形式存在 进程process:程序通过用户执行被触发后,执行者的权限与属性.程序的代码和所需数据会被加载到内存中,OS给予这个 ...

  6. Hive是什么

    Hive是什么1)Hive 是建立在Hadoop (HDFS/MR)上的用于管理和查询结果化/非结构化的数据仓库:2)一种可以存储.查询和分析存储在Hadoop 中的大规模数据的机制:3)Hive 定 ...

  7. Golang panic用法

    Go语言追求简洁优雅,所以,Go语言不支持传统的 try…catch…finally 这种异常,因为Go语言的设计者们认为,将异常与控制结构混在一起会很容易使得代码变得混乱.因为开发者很容易滥用异常, ...

  8. 订阅 memcached: error while loading shared libraries: libevent-2.0.so.5: cannot o解决

    memcached: error while loading shared libraries: libevent-2.0.so.5: cannot o解决   memcached基本选项 -p 端口 ...

  9. sql server 运算

    --Sql Server 乘法运算--select (d.RepaymentSchedule*d.MonthlyPayment) as ExpectedReceivablePayment from T ...

  10. 手把手教你写一个java的orm(一)

    写之前的说明 其实吧. 这个东西已经写好了,地址在:https://github.com/hjx601496320/JdbcPlus 这系列文章算是我写的过程的总结吧.(恩系列,说明我可能会写好久,╮ ...