2016-11-09 6 views
1

Swift 3とAlamofireを使用しているアプリがあります。データは、2つのcell?.viewWithTag(2)cell?.viewWithTag(1)(URLからの画像)とテキストに接続されています。だから私がプロジェクトを実行するときに何も私のアプリケーションに表示されます。 print(readableJSON)でJSONをテストし、JSONがコンソールに印刷されています。だから私は本当に少し混乱している。あなたがテーブルセルであなたのJSONが表示されていないため、Swift 3とAlamofireでJSON URLからアプリに何も表示されない

SWIFT

import UIKit 
import Alamofire 

struct postinput { 
    let mainImage : UIImage! 
    let name : String! 

} 


class TableViewController: UITableViewController { 

    var postsinput = [postinput]() 

    var mainURL = "https://www.example.api.com" 

    typealias JSONstandard = [String : AnyObject] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     callAlamo(url: mainURL) 
    } 

    func callAlamo(url : String){ 
     Alamofire.request(url).responseJSON(completionHandler: { 
      response in 

      self.parseData(JSONData: response.data!) 


     }) 

    } 

    func parseData(JSONData : Data) { 
     do { 
      var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONstandard 
     print(readableJSON) 

      if let posts = readableJSON["posts"] as? [JSONstandard] { 
       for post in posts { 
        let title = post["title"] as! String 
        print(title) 

        if let imageUrl = post["image"] as? JSONstandard { 
         let mainImageURL = URL(string: imageUrl["url"] as! String) 
         let mainImageData = NSData(contentsOf: mainImageURL!) 


         let mainImage = UIImage(data: mainImageData as! Data) 
         postsinput.append(postinput.init(mainImage: mainImage, name: title)) 
         self.tableView.reloadData() 

        } 


       } 

      } 

     } 


     catch { 
      print(error) 
     } 


    } 




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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 

     // cell?.textLabel?.text = titles[indexPath.row] 

     let mainImageView = cell?.viewWithTag(2) as! UIImageView 

     mainImageView.image = postsinput[indexPath.row].mainImage 

     let mainLabel = cell?.viewWithTag(1) as! UILabel 

     mainLabel.text = postsinput[indexPath.row].name 


     return cell! 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

JSON

{ 
    "posts" : [{ 
     "id": "000000", 
     "url": "/content/interview2", 
     "date": "2016-11-03 09:01:41", 
     "modified": "2016-11-03 09:03:47", 
     "title": "An interview", 
     "image": "https://www.example.com/sites/default/files/oregood.jpeg", 
     "summary": { 
      "value": "<p>Latin text here</p>", 
      "format": "filtered_html" 
     } 
    }] 
} 
+0

jasonが表示されますが、データを表ビューで表示できません。 – Mohit

+0

正しいMohit .. – rob

+0

「numberOfSectionsInTableView」メソッドが表示されませんでした。どこですか? –

答えて

1

imageキーがStringないDictionaryはまた、あなたがループのないループ内のすべての時間のためにあなたのtableView外をリロードするので、このようにしてみてくださいする必要が含まれてい。

if let posts = readableJSON["posts"] as? [JSONstandard] { 
    for post in posts { 
     let title = post["title"] as! String    
     if let imageUrl = post["image"] as? String { 
      let mainImageURL = URL(string: imageUrl as! String) 
      let mainImageData = NSData(contentsOf: mainImageURL!) 
      let mainImage = UIImage(data: mainImageData as! Data) 
      postsinput.append(postinput.init(mainImage: mainImage, name: title))     
     } 
    } 
    DispatchQueue.main.async { 
     self.tableView.reloadData() 
    } 
} 

提案:代わりSDWebImagesのようなライブラリを使用するか、独自の非同期画像ダウンローダを作成することができ、メインスレッドの打者にNSData(contentsOf:)を使用して画像をダウンロードします。

+0

それはNiravを働いた...ありがとう! – rob

0

:私のSWIFTはこのようになります。あなたはクレート「mainImageView」と呼ばれ、テーブルセルの実際のImageViewのようにそれを割り当てることはありませんコピーされたオブジェクトは、代わりにしてみてください:

(cell?.viewWithTag(2) as? UIImageView).image = postsinput[indexPath.row].mainImage 

postsinputは、画像文字列や画像が含まれていますか?

EDIT: は、それは次のようになります。あなたのJSON応答から

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 

     // cell?.textLabel?.text = titles[indexPath.row] 

     //let mainImageView = cell?.viewWithTag(2) as! UIImageView 

     //mainImageView.image = postsinput[indexPath.row].mainImage 
     (cell?.viewWithTag(2) as? UIImageView).image = postsinput[indexPath.row].mainImage 

     //let mainLabel = cell?.viewWithTag(1) as! UILabel 

     //mainLabel.text = postsinput[indexPath.row].name 
     (cell?.viewWithTag(1) as? UILabel).text = postsinput[indexPath.row].name 

     return cell! 
    } 
+0

どのような時点でそのコードはRepoguyになりますか? mainImageView = cell?.viewWithTag(2)を!にする代わりに、オーバーライド関数tableView( – rob

+0

)内のtableView(_ tableView:UITableView、cellForRowAt indexPath:IndexPath) - > UITableViewCell { UIImageView AND mainImageView.image = postsinput [indexPath.row] .mainImage 同じことがラベルに適用されます – repoguy

+0

ありがとうございますが問題は解決しませんでした。しかし、その解決した今Repoguy – rob

関連する問題