2017-08-30 8 views
0

無限スクロールと「プッシュ・リフレッシュ」のためにWebからデータを呼び出しましたが、無限スクロールはうまく機能しますが、「プル・トゥ・リフレッシュ」は機能しません私はrefreshAction funcで書かなければならないコードの種類を知らない。無限スクロールのJSONリクエストでリフレッシュする機能が動作しない

NetworkRequestAPI要求

import Foundation 

class NetworkRequestAPI { 

static func getPropductListByCategory(productId : Int, pageNo : Int , completion: @escaping ([Product]? , Error?) ->()){ 



    let url = URL(string: Configuration.BASE_URL+"/product-by/type?product_type_id="+String(productId)+"&page="+String(pageNo)) 



    var categoryObject = [Product]() 



    URLSession.shared.dataTask(with:url!) { (urlContent, response, error) in 

     if error != nil { 


     } 
     else { 
      do { 
       let json = try JSONSerialization.jsonObject(with: urlContent!) as! [String:Any] 



       let products = json["products"] as? [String: Any] 
       // productCount = (json["product_count"] as? Int)! 

       let items = products?["data"] as? [[String:Any]] 



       items?.forEach { item in 

        let oProduct = Product() 

        oProduct.product_id = item["product_id"] as? Int 
        oProduct.product_name = item["product_name"] as? String 

        oProduct.product_image = item["product_image"] as? String 



        let ratingItem = item["rating_info"] as? [String: AnyObject] 

        let rating = RatingInfo() 
        rating.final_rating = ratingItem?["final_rating"] as? String 

        oProduct.rating_info = rating 


        categoryObject.append(oProduct) 
       } 
       completion(categoryObject, nil) 

      } catch let error as NSError { 

       print(error) 
       completion(nil, error) 
      } 
     } 


     }.resume() 
} 


} 

ListTableViewクラス

class ListTableView: UITableViewController { 



    var isInitUILoad = true 

    var arrProduct = [[Product]]() 

    var product_id:Int = 0 

    var pageNo = 1 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.initUILoad() 




     let refreshControl = UIRefreshControl() 


     if #available(iOS 10.0, *) { 
      tableView.refreshControl = refreshControl 
     } else { 
      tableView.addSubview(refreshControl) 
     } 


     refreshControl.addTarget(self, action: #selector(refreshWeatherData(_:)), for: .valueChanged) 


    } 


    @objc private func refreshWeatherData(_ sender: Any) { 

     // 
     // what codes have to add here 
     // 


     self.refreshControl?.endRefreshing() 
    } 



func initUILoad(){ 

    ActivityIndicator.customActivityIndicatory(self.view, startAnimate: true) 

    NetworkRequestAPI.getPropductListByCategory(productId: product_id, pageNo: pageNo) { (products, error) in 

     DispatchQueue.main.async(execute: { 
      if products != nil { 
       // self.totalItemLabel.text = String(self.product_count) + " products" 
       self.pageNo += 1 
       // self.product_count = productCount 
       //self.arrProduct = products! 
       self.arrProduct.append(products!) 
       print(self.arrProduct.count) 
       // self.tableView?.reloadData() 
      }else{ 
       print(error.debugDescription) 

      } 

      ActivityIndicator.customActivityIndicatory(self.view, startAnimate: false) 
      // self.totalItemLabel.text = String(self.product_count) + " products" 
      self.tableView?.reloadData() 
      self.isInitUILoad = false 
     }) 
    } 

} 

func loadMore(complition:@escaping (Bool) ->()) { 

    NetworkRequestAPI.getPropductListByCategory(productId: product_id, pageNo: pageNo) { (products, error) in 

     DispatchQueue.main.async(execute: { 
      if error != nil{ 
       print(error.debugDescription) 
      } 
      if products != nil && products?.count ?? 0 > 0{ 
       // self.totalItemLabel.text = String(self.product_count) + " products" 
       self.pageNo += 1 
       // self.product_count = productCount 
       // self.arrProduct = products! 
       self.arrProduct.append(products!) 
       self.tableView?.insertSections([self.arrProduct.count - 1], with: .fade) 
       // self.tableView?.reloadData() 

      }else{ 
       print("no product left") 

      } 
      complition(true) 

     }) 
    } 
} 

} 

答えて

0
@objc private func refreshWeatherData(_ sender: Any) { 

    NetworkRequestAPI.getPropductListByCategory(productId: product_id, pageNo: 1) { (products, error) in 

     DispatchQueue.main.async(execute: { 
      if products != nil { 
       // self.totalItemLabel.text = String(self.product_count) + " products" 

       self.arrProduct = products! 
       print(self.arrProduct.count) 

      }else{ 
       print(error.debugDescription) 

      } 

      self.refreshControl?.endRefreshing() 
      // self.totalItemLabel.text = String(self.product_count) + " products" 
      self.tableView?.reloadData() 
      self.isInitUILoad = false 
     }) 
    } 
} 
+0

弟!どのようにフッターにアクティビティを置くことができますか? –

+0

@Minzuアクティビティインジケータでフッタービューを表示することでこれを実現できます または、ヘルプを得ることができます。 https://github.com/vlasov/CCBottomRefreshControl –

+0

ブラザー!それは客観的なCです。私は客観的なcを知らない。フッタのロジックコードについて教えてください。私はフッタに書く必要があります。 –

関連する問題