更新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. web前端之JavaScript

    JavaScript概述 JavaScript历史 在上个世纪的1995年,当时的网景公司正凭借其Navigator浏览器成为Web时代开启时最著名的第一代互联网公司.由于网景公司希望能在静态HTML ...

  2. java.lang.ArithmeticException: Rounding necessary

    这个错误就是精度丢失问题 https://blog.csdn.net/qq496013218/article/details/70792655

  3. OOAD之面向对象设计原则

    学习这个设计模式 真的觉得很抽象,只有自己多多的领会! 在很多时候,很多的知识都会觉得讲起来是很矛盾的. 本章目标 1 掌握内聚度和耦合度的概念 2 掌握面向对象设计原则  (一)如何衡量软件设计的质 ...

  4. 功能------常用快捷键(在win10下)

    功能------win10 常用快捷键 在进行学习,记录,编写代码时,需要用到一些功能,用鼠标浪费时间,可以使用快捷键来快速的处理.方便操作. 以下内容分为两类.快捷键以及触控板类(不能享用鼠标) 快 ...

  5. [HAOI 2015]树上染色

    Description 题库链接 给出一棵 \(n\) 个节点的树,边有权值.让你将树上 \(k\) 个点染黑,剩余 \(n-k\) 个点染白.染色后记一种染色方案的价值为黑点间两两距离和以及白点间两 ...

  6. sql server 运算

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

  7. 湘潭校赛 Bob's Problem

    Bob's Problem Accepted : 18   Submit : 115 Time Limit : 1000 MS   Memory Limit : 65536 KB  题目描述 Bob今 ...

  8. 浅谈TCP/IP(new 常见面试问题)

    1. TCP/IP重传机制,如何保证消息读到一个完整内容再反序列化 2. TCP四次回收比三次握手多了什么操作,什么时候会进入Time_await状态 3.

  9. FIO测试磁盘的iops

    FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, null, ...

  10. php5.5过渡--mysql连接

    以前: // $conn=mysql_connect("localhost","root","");// $db=mysql_select_ ...