2017-05-01 113 views
-1

我試圖在桌面視圖中顯示來自api的圖像,但我沒有得到圖像或任何東西。 ImageTableViewCell只有圖像的出口。在UITableview中顯示圖像

import UIKit 
import Alamofire 
import SwiftyJSON 
import Haneke 

class SlideViewController: UIViewController , UITableViewDelegate , UITableViewDataSource { 

@IBOutlet weak var tableview : UITableView! 

var images = [String]() 




override func viewDidLoad() { 
    super.viewDidLoad() 

    tableview.delegate = self 
    tableview.dataSource = self 

    getJSON() 


} 


func numberOfSections(in tableView: UITableView) -> Int { 
    return 0 
} 


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



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell", for: indexPath) as! ImageTableViewCell 


    let url = URL(string: images[indexPath.row]) 

    cell.images.hnk_setImage(from: url!) 

    return cell 
} 




func getJSON() { 

    let url = "http://localhost:8000/api/hello" 
    request(url , method: .get, encoding: JSONEncoding.default ) 
     .responseJSON { response in 

      if let value: AnyObject = response.result.value as AnyObject? { 
       //Handle the results as JSON 
       do{ 
        if let albums = try JSONSerialization.jsonObject(with: response.data!, options: []) as? [String: Any], 
         let pics = albums["pic"] as? [Any] { 

         self.images = pics as! [String] 

         for kick in pics { 
          self.images.append(kick as! String) 

         } 

        } 

       }catch { 
        print("Error with Json: \(error)") 
       } 
       DispatchQueue.main.async { 
        self.tableview.reloadData() 
       } 

    } 
    } 
    } 

    } 

任何幫助將不勝感激。我嘗試了很多方法,但這似乎很簡單,並且易於跟蹤

+0

在這一行你的應用程序崩潰了? self.images.append(album [「pic」] as!String) – KKRocks

+0

yes你是對的@KKRocks – leo0019

+0

這行是什麼:self.images.append(album [「pic」] as!字符串) – KKRocks

回答

0

jsonObject(with:options:)[String:Any]結果,而不是[[String: AnyObject]]您必須至少有一個部分展現。

func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 
+0

我怎麼忘記這個笑聲 – leo0019

+1

不需要numberofSections方法。 Tableview默認已經有一個部分。 –

+0

@ leo0019這是完全錯誤的答案,如果你只有一個部分,那麼就不需要這個方法 –

1

您的JSON響應是Dictionary而不是Array,其中包含pic數組。所以,鍵入

do{ 
    if let albums = try JSONSerialization.jsonObject(with: response.data!, options: []) as? [String: Any], 
     let pics = albums["pics"] as? [Any] { 

     images = pics.flatMap{ $0 as? String } 
    } 
}catch { 
    print("Error with Json: \(error)") 
} 
DispatchQueue.main.async { 
    self.tableview.reloadData() 
} 
+0

我嘗試,但它給我一個錯誤,我印刷專輯 「> [ 「PIC」:<__ NSArrayI 0x6000002873f0>( HTTP://本地主機:8000 /圖像/ 1490104055.jpg, HTTP://本地主機: 8000/images/1490104055.jpg, http:// localhost:8000/images/1490104055.jpg, http:// localhost:8000/images/1490104055.jpg, http:// localhost:8000/images/1490104055 .JPG, HTTP://本地主機:8000 /圖像/ 1490104055.jpg, HTTP://本地主機:8000 /圖像/ 1490104055.jpg ) ] 致命錯誤:意外地發現零而展開的可選值 ( lldb)' – leo0019

+0

print(i mages] = []沒有圖像顯示! – leo0019

+0

還沒有圖片 – leo0019

1

檢查下面的代碼

var images = [String]() 

func getJSON() { 

    let url = "http://localhost:8000/api/hello" 
    request(url , method: .get, encoding: JSONEncoding.default ) 
     .responseJSON { response in 

      if let value: AnyObject = response.result.value as AnyObject? { 
       //Handle the results as JSON 
       do{ 
        let albums = try JSONSerialization.jsonObject(with: response.data!, options:.allowFragments) as! [[String: AnyObject]] 
        print(albums) 
        if let pics = album["pic"] 
        { 
         for album in pics 
         { 
          images.append(album) 
         } 
        } 

       }catch { 
        print("Error with Json: \(error)") 
       } 
       self.tableview.reloadData() 

      } 
    } 
} 
+1

不能下標類型的值索引爲'String'的[[String:AnyObject]] 此錯誤出現在if let pics = albums [「pic」] – leo0019

+0

@ leo0019輸出的響應是什麼?你能在這裏評論嗎? ** {在** –

+0

SUCCESS響應:{ PIC =( 「HTTP://本地主機:8000 /圖像/ 1490104055.jpg」, 「HTTP://本地主機:8000 /圖像/ 1490104055.jpg」 , 「http:// localhost:8000/images/1490104055.jpg」, 「http:// localhost:8000/images/1490104055.jpg」, 「http:// localhost:8000/images/1490104055.jpg 「, 」http:// localhost:8000/images/1490104055.jpg「, 」http:// localhost:8000/images/1490104055.jpg「 ); } – leo0019