IOS Swift TableViewCell 加载网络图片后图片高度
2015-11-30 16:15
369 查看
有时候TableView需要加载网络图片,但网络图片的大小不一,所以显示的时候需要根据图片的大小动态的改变TableViewCell 的高,如下图所示:
如果要实现右图的效果:
需要建立一个数组,用于保存每个cell的高度,可以给一个默认值,在网络请求图片后,根据返回的imaga判断图片的尺寸大小,重新修改数组里面对应的cell的高。我用的是
Alamofire第三方库请求数据,一下是关键代码:
override
func tableView(tableView:
UITableView, heightForRowAtIndexPath indexPath:
NSIndexPath) -> CGFloat {
return
CGFloat(self.testHList[indexPath.row])//testHList存放cell的高,更新cell
}
override
func tableView(tableView:
UITableView, cellForRowAtIndexPath indexPath:
NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("testCellIndentifier",
forIndexPath: indexPath) as!
TestTableViewCell
cell.titleLabel.text =
"Uncomment the following line to preserve selection between presentations"
request(.GET,testList[indexPath.row]).response
{ (_, response, data, _) ->
Void in
if
let image = UIImage(data: data!) {
cell.testImageView.image = image
let screen_width =
UIScreen.mainScreen().bounds.width
let oldHeight =
self.testHList[indexPath.row]
var newHeight =
Int(image.size.height)
if image.size.width > screen_width {
let aspect = image.size.height / image.size.width
newHeight = Int(screen_width * aspect)
}
if oldHeight != newHeight {
self.tableView.beginUpdates()
self.testHList[indexPath.row] = newHeight//设置cell的高时需要beginUpdates()和endUpdates()之间,这样界面才能刷新
self.tableView.endUpdates()
}
}
}
如果要实现右图的效果:
需要建立一个数组,用于保存每个cell的高度,可以给一个默认值,在网络请求图片后,根据返回的imaga判断图片的尺寸大小,重新修改数组里面对应的cell的高。我用的是
Alamofire第三方库请求数据,一下是关键代码:
override
func tableView(tableView:
UITableView, heightForRowAtIndexPath indexPath:
NSIndexPath) -> CGFloat {
return
CGFloat(self.testHList[indexPath.row])//testHList存放cell的高,更新cell
}
override
func tableView(tableView:
UITableView, cellForRowAtIndexPath indexPath:
NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("testCellIndentifier",
forIndexPath: indexPath) as!
TestTableViewCell
cell.titleLabel.text =
"Uncomment the following line to preserve selection between presentations"
request(.GET,testList[indexPath.row]).response
{ (_, response, data, _) ->
Void in
if
let image = UIImage(data: data!) {
cell.testImageView.image = image
let screen_width =
UIScreen.mainScreen().bounds.width
let oldHeight =
self.testHList[indexPath.row]
var newHeight =
Int(image.size.height)
if image.size.width > screen_width {
let aspect = image.size.height / image.size.width
newHeight = Int(screen_width * aspect)
}
if oldHeight != newHeight {
self.tableView.beginUpdates()
self.testHList[indexPath.row] = newHeight//设置cell的高时需要beginUpdates()和endUpdates()之间,这样界面才能刷新
self.tableView.endUpdates()
}
}
}
相关文章推荐
- Eclipse下搭建Android环境的网络配置
- 网络爬虫结合搜索引擎
- 深入理解HTTP协议、HTTP协议原理分析
- 笔记—TCP有限状态机分析
- 笔记—TCP有限状态机分析
- 使用loadrunner对https协议(单双向SSL)的web端性能测试 (转)
- 简单的网络爬虫
- 网络性能测试工具iperf详细使用图文教程
- (iOS)网络数据传输-01
- https原理:证书传递、验证和数据加密、解密过程解析 (转)
- 复习后台代码(与前面clentHttp连接网络结合)
- 通过HttpClient方式连接网络
- IOS学习之 网络编程(11)--使用ASI框架进行文件下载
- 一个判断网络类型的工具类(wifi 2g 3g 4g)
- ios学习之 网络编程(9)--数据缓存
- IOS学习之 网络编程(8)--发送json数据给服务器以及多值参数
- HttpWebRequest.Proxy属性
- IOS学习之 网络编程(7)--监测网络状态
- IOS学习之 网络编程(10)--简单介绍ASI框架的使用
- VMware中使用NAT网络在虚拟机Linux上联网