Swift實戰-豆瓣電台(四)歌曲列表的展現


觀看地址 http://v.youku.com/v_show/id_XNzMwNDE0OTA4.html

這節的主要內容是如何利用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 NSDictionary
cell.text=rowData["title"] as String
cell.detailTextLabel.text=rowData["artist"] as NSString

接着我們在顯示縮略圖的時候先給一張默認的圖片

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

然后我們去抓取網絡圖片,同樣,是用異步的方式。為了提高性能,我們對獲取的圖片做了一個緩存

var imageCache = Dictionary<String,UIImage>()

通過圖片的地址來緩存UIImage

 

        let url=rowData["picture"] as String
        let image=self.imageCache[url] as?UIImage
        if !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 in
                var img=UIImage(data:data)
                cell.image=img
                self.imageCache[url]=img
            })
        }else{
            cell.image=image
        }  

 

本節高清視頻和代碼下載地址

http://pan.baidu.com/s/1sjHd5qX

下一節,我們將一起學習一下播放歌曲和展示當前歌曲圖片

 

Swift實戰-豆瓣電台系列

Swift實戰-豆瓣電台(一)准備

Swift實戰-豆瓣電台(二)界面布局

Swift實戰-豆瓣電台(三)獲取網絡數據

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM