UICollectionView上的Pull to Refresh - Swift 3 - 无法正常工作

时间:2017-09-28 02:34:26

标签: ios swift uicollectionview

我正在尝试使用swift视图在UiCollectionView上实现 pull to refresh

我试过,当我拉动时,我可以看到显示进度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。

1 个答案:

答案 0 :(得分:0)

找到解决方案

df = pd.DataFrame({'feature1': [0,2,1,2], 'feature2': [1,2,3,4],'feature3':[2,'?','?','?'],'feature4':[100,900,861,'?']})