gpt4 book ai didi

ios - 拉动刷新 UICollectionView - Swift 3 - 不工作

转载 作者:行者123 更新时间:2023-11-28 08:01:48 25 4
gpt4 key购买 nike

我正在尝试使用快速 View 在 UiCollectionView 上实现拉动刷新

我已经尝试过,当我拉动时,我可以看到显示进度 gif 图像的 subview 。但它没有进入选择器方法 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()
})
}

}

谁能帮我解决这个问题。谢谢。

最佳答案

找到解决方案

    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()
}

关于ios - 拉动刷新 UICollectionView - Swift 3 - 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46460076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com