2017-06-21 5 views
1
import UIKit 

内の別のページに戻ってきて複製されている私の配列リストはここ表の表示セルは、後にここで迅速

var postIdArray :[String] = [] 
    var adminIdArray :[String] = [] 
    var titleArray :[String] = [] 
    var descriptionArray :[String] = [] 
    var ImageArray :[String] = [] 
    var postDate :[String] = [] 
    var myIndex = 0 

    class News__Latest_News_: BaseViewController, UITableViewDelegate, UITableViewDataSource { 
     @IBOutlet var tabelView: UITableView! 
     final let urlString = "http://iccukapp.org/Api_json_format/" 

     override func viewDidLoad() { 
      super.viewDidLoad() 
      self.downloadJsonWithURL() 
     } 

である私は、テーブルビューの設定ここで

 func downloadJsonWithURL() { 

      let url = NSURL(string: urlString) 
      URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in 

       if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary { 
        print(jsonObj!.value(forKey: "result") as Any) 

        if let ListArray = jsonObj!.value(forKey: "result") as? NSArray { 
         for eList in ListArray{ 
          if let eventIDD = eList as? NSDictionary { 
          if let name = eventIDD.value(forKey: "post_id") { 
            postIdArray.append(name as! String) 
           } 
         if let name = eventIDD.value(forKey: "admin_id") { 
            adminIdArray.append(name as! String) 
           } 
         if let name = eventIDD.value(forKey: "title") { 
            titleArray.append(name as! String) 
           } 
         if let name = eventIDD.value(forKey: "description") { 
            descriptionArray.append(name as! String) 
           } 
         if let name = eventIDD.value(forKey: "post_iamge") { 
            ImageArray.append(name as! String) 
           } 
        if let name = eventIDD.value(forKey: "post_created") { 
            postDate.append(name as! String) 
           } 


          } 
         } 
        } 

        OperationQueue.main.addOperation({ 
         self.tabelView.reloadData() 


        }) 
       } 
      }).resume() 
     } 

JSONをダウンロード

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

**ここでの設定は、テーブルビューセル用です**

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

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

      cell.nameLabel.text = titleArray[indexPath.row] 
      let alinkurl = "http://iccukapp.org/assets/admin/images/" 

      let imagUrl = NSURL(string: "\(alinkurl)" + ImageArray[indexPath.row]) 


      let qos = DispatchQoS(qosClass: .background, relativePriority: 0) 
      let backgroundQueue = DispatchQueue.global(qos: qos.qosClass) 
      backgroundQueue.async { 
       if imagUrl != nil { 
        let data = NSData(contentsOf: (imagUrl as URL?)!) 
        cell.imageLabel.image = UIImage(data: data! as Data) 

       } 

      } 

      return cell 

     } 

アプリ初めてテーブルビューを実行した後セグエ

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
      myIndex = indexPath.row 
      performSegue(withIdentifier: "newsId", sender: self) 


     } 

    } 

を実行するためのこのfuncが正しい内容を示しています。しかし、私が他のページに行き、このページに戻ると、すべてのコンテンツが複製されます。 高度なおかげ

+0

'viewDidLoad'以外の場所から' downloadJsonWithURL'を呼び出していますか? – paulvs

答えて

0

私は(FUNCのdownloadJsonWithURLを疑うが)を2回呼び出されていると、これはあなたがテーブルを移入するために使用配列にデータを追加するので、あなたは重複

てみ設定を参照してください。

self.postIdArray = [] 
self.adminIdArray = [] 
self.titleArray = [] 
self.descriptionArray = [] 
self.ImageArray = [] 
self.postDate = [] 

JSON呼び出しの結果を追加する前に空の配列を設定するには、downloadJsonWithURL()を呼び出してください。

関連する問題