2017-09-28 9 views
1

私は即時ビューを使用してUICollectionView上のをリフレッシュするためにプルしようとしています。UICollectionView - Swift 3 - 動作しませんでリフレッシュする

私はプルをするときに、私は進行状況のGIF画像を示すサブビューを見ることができます。セレクタメソッドloadData()に入っていないか、データをリロードしていません。

enter image description here

DashBoardCollectionVCは

import UIKit 
import SwiftyJSON 

class DashBoardCollectionVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 

    @IBOutlet weak var main_collection_view: UICollectionView! 

    var refresher:UIRefreshControl! 

    var sensorObjectList = [SensorObject]() 
    var collectionViewLayout: CustomImageFlowLayout! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     collectionViewLayout = CustomImageFlowLayout() 
     main_collection_view.collectionViewLayout = collectionViewLayout 

     initiateRefreshData() 
     getCustomGroupSensors() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return sensorObjectList.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     var batteryLevel: String = "" 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identify_collection_cell", for: indexPath) as! DashboardCollectionViewCell 

     let imageName = ApplicationUtility().getSensorImageName(icon: sensorObjectList[indexPath.row].icon, type: sensorObjectList[indexPath.row].type, state: sensorObjectList[indexPath.row].state, hardware_status: sensorObjectList[indexPath.row].hardware_status) 

     cell.img_sensor.image = UIImage(named: imageName) 
     cell.img_battery.image = UIImage(named: ApplicationUtility().getBatteryImage(batterLevel: sensorObjectList[indexPath.row].batteryLevel)) 

     batteryLevel = (sensorObjectList[indexPath.row].batteryLevel == 404) ? "error" : String(sensorObjectList[indexPath.row].batteryLevel)+"%" 
     cell.lbl_battery_level.text = batteryLevel 
     cell.lbl_sensor_name.text = sensorObjectList[indexPath.row].label 
     cell.lbl_sensor_status.text = ApplicationUtility().getSensorStatusValue(icon: sensorObjectList[indexPath.row].icon, state: sensorObjectList[indexPath.row].state, hardware_status: sensorObjectList[indexPath.row].hardware_status) 


     if sensorObjectList[indexPath.row].batteryLevel < 20 || sensorObjectList[indexPath.row].batteryLevel == 404 { 
      cell.lbl_battery_level.textColor = UIColor.red 
     } 
     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     print("selected item : \(indexPath.row)") 
     if sensorObjectList[indexPath.row].is_read_only { 

      let alert = UIAlertController(title: "No Action", message: "Read only sensor", preferredStyle: UIAlertControllerStyle.alert) 
      alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.default, handler: nil)) 
      self.present(alert, animated: true, completion: nil) 
     } else { 

      let sendingParaMeter = (sensorObjectList[indexPath.row].state == "OFF") ? "ON" : "OFF" 

      MainUtility().requestUsingPostMethodSensorAction(url:sensorObjectList[indexPath.row].link, parameter: sendingParaMeter, completion: { response in 
       self.sensorObjectList[indexPath.row].state = sendingParaMeter 
       print("CMD Executed   : Current State of \(self.sensorObjectList[indexPath.row].label) is : \(self.sensorObjectList[indexPath.row].state)") 

       self.reloadData() 

      }) 
      } 
    } 

    func reloadData() -> Void { 
     DispatchQueue.main.async() { 
      self.main_collection_view.reloadData() 
     } 
    } 

    func initiateRefreshData()-> Void { 
     self.refresher = UIRefreshControl() 
     self.main_collection_view!.alwaysBounceVertical = true 
     self.refresher.tintColor = UIColor.red 
     self.refresher.addTarget(self, action: #selector(self.reloadData), for: .valueChanged) 
     self.main_collection_view!.addSubview(refresher) 
    } 

    func loadData() -> Void { 
     getCustomGroupSensors() 
     refresher.endRefreshing() 
    } 


    override var prefersStatusBarHidden : Bool { 
     return true 
    } 


    func getCustomGroupSensors() -> Void { 
     //Get custom groups layout with members 
     MainUtility().requestUsingGetMethod(url: "/rest/octopus/layout/groups/custom/members", completion:{ response in 
      print("--------------------- custom groups layout with members -------------------------") 
      print(response) 
      for result in response.array!{ 

       for members in result["members"].array!{ 
        let sensor = SensorObject() 

        var sensor_tags = [Int]() 
        var sensor_groupNames = [String](); 

        for i in 0..<members["tags"].count{ 
         sensor_tags.append(members["tags"].arrayValue[i].intValue) 
        } 

        for i in 0..<members["groupNames"].count{ 
         sensor_groupNames.append(members["groupNames"].arrayValue[i].stringValue) 
        } 

        sensor.tags = sensor_tags 
        sensor.groupNames = sensor_groupNames 
        sensor.link = members["link"].stringValue 
        sensor.icon = members["icon"].stringValue 
        sensor.label = members["label"].stringValue 
        sensor.type = members["type"].stringValue 
        sensor.batteryLevel = members["batteryLevel"].intValue 
        sensor.state = members["state"].stringValue 
        sensor.name = members["name"].stringValue 
        sensor.hardware_status = members["hardware"]["status"].stringValue 
        sensor.hardware_status_detail = members["hardware"]["statusDetail"].stringValue 
        sensor.is_read_only = members["stateDescription"]["readOnly"].boolValue 
        self.sensorObjectList.append(sensor) 
       } 
      } 
      self.reloadData() 
     }) 
    } 

} 

誰かがこれを修正するために私を助けることができます。 tnx。

答えて

0

は、溶液中に

func initiateRefreshData()-> Void { 

     let refreshControl = UIRefreshControl() 
     let title = NSLocalizedString("Refreshing sensor data...", comment: "Pull to refresh") 
     refreshControl.attributedTitle = NSAttributedString(string: title) 
     refreshControl.tintColor = UIColor.red 
     refreshControl.addTarget(self, action: #selector(refreshOptions(sender:)), 
           for: .valueChanged) 
     self.main_collection_view.refreshControl = refreshControl 
    } 

    @objc private func refreshOptions(sender: UIRefreshControl) { 
     getCustomGroupSensors() 
     sender.endRefreshing() 
    } 
を発見しました
関連する問題