[Swift实战]豆瓣电台

标题歌曲列表的展现



这节的主要内容是如何利用cell展现获取到的数据。

首先申明两个数组来储存我们获取到的数据

var tableData:NSArray=NSArray()var channelData:NSArray=NSArray()

tableData是主界面上歌曲列表要用的数据。所以在func tableView(tableView: UITableView!, numberOfRowsInSection section: Int)这个方法中要返回tableData的数量

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{return tableData.count} 

然后我们把tableData中的数据填充到主界面的TableView中

let cell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "douban")let rowData:NSDictionary=self.tableData[indexPath.row] as NSDictionarycell.text=rowData["title"as Stringcell.detailTextLabel.text=rowData["artist"as NSString

接着我们在显示缩略图的时候先给一张默认的图片

cell.image=UIImage(named:"detail.jpg")

然后我们去抓取网络图片,同样,是用异步的方式。为了提高性能,我们对获取的图片做了一个缓存

var imageCache = Dictionary<String,UIImage>()

通过图片的地址来缓存UIImage

复制代码
        let url=rowData["picture"as Stringlet image=self.imageCache[url] as?UIImageif !image?{let imgURL:NSURL=NSURL(string:url)let request:NSURLRequest=NSURLRequest(URL: imgURL)NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response:NSURLResponse!,data:NSData!,error:NSError!)->Void invar img=UIImage(data:data)cell.image=imgself.imageCache[url]=img})}else{cell.image=image}  
复制代码

下一节,我们将一起学习一下播放歌曲和展示当前歌曲图片



上一课时: 下一课时:

相关课程

课时简介

课时发布人:admin

发布于:2015-01-08

363 0

课程创建人

最近最近学完的人( 全部1人 )

最近收藏该课时的人( 全部1人 )

立即注册
学习路上要有伙伴同行!
登录后可与26名学员及知识达人交流、学习。