簡體   English   中英

讀取遠程JSON並從URL獲取圖像

[英]Reading Remote JSON and Getting Image from URL

在我的iOS應用中,我試圖讀取JSON文件並使用其內容填充表格。 以下是JSON文件的示例。

目前,我正在將JSON讀入一個稱為“ items”的可變數組中,然后像這樣填充表格單元格。

// Populate the cell
if let showName = self.items[indexPath.row]["Show"] as? NSString {
            cell.textLabel!.text = showName as! String

我希望每個JSON記錄的圖像也顯示在單元格中,這就是我被絆倒的地方。

我有圖像的URL,但是如何將其放入表格單元格?

我的整個方法可能是錯誤的,因此,我希望能指出正確的方向。

{
        "shows": [{
                "Day": "Sunday",
                "Time": "12am",
                "Show": “Show Name Here",
                "imgPath": "http://remoteserver/image.jpg"
        }, {
                "Day": "Sunday",
                "Time": "1am",
                "Show": "Show Name Here",
                "imgPath": “http://remoteserver/image.jpg"

        }, {
                "Day": "Sunday",
                "Time": "2am",
                "Show": "Show Name Here",
                "imgPath": http://remoteserver/image.jpg"

        }

我建議為此使用SDWebImage之類的庫。 這將為您提供將圖像異步加載到imageview的方法,並允許您在下載圖像時設置占位符。 您可以通過以下網址設置圖片(從SDWebImage文檔復制):

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                  placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

更多信息在這里: https : //github.com/rs/SDWebImage

蘋果公司有一個使用RSS的示例項目。 LazyTableImages此代碼不是很快,但將確切顯示Apple如何處理此代碼。

聽起來您遇到的問題是單元正在被重用並且圖像沒有被清除或被添加到重用的單元中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM