2017-05-01 117 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