gpt4 book ai didi

ios - 如何从 Swift 中的另一个类重新加载 Collection View Cells

转载 作者:搜寻专家 更新时间:2023-11-01 07:29:49 24 4
gpt4 key购买 nike

我尝试在接收到一些 BLE 数据时重新加载 CollectionView。这是我的 BluetoothManager 类:

import UIKit
import CoreBluetooth

class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
static var bluetoothManager = BluetoothManager()
...
var Value: [String] = [""]
...
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?){
//For this example, there is only a string value to update
Value[0] = "It works!"
}}

一切正常,当我收到一些数据时,didUpdateValueForCharacteristic 函数被调用,Value 数组的第一项是字符串“It works”。现在我想刷新/重新加载我的 CollectionView Controller ,以显示新值。这是我的收藏 View 类:

import UIKit

class ValueCollectionView: UICollectionViewController{
@IBOutlet var ValueCollectionView: UICollectionView!

var valueCollectionViewCell: ValueCollectionViewCell = ValueCollectionViewCell()

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

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

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return BluetoothManager.bluetoothManager.Value.count
}

override func collectionView(tableView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell: ValueCollectionViewCell = collectionView?.dequeueReusableCellWithReuseIdentifier("valueCell", forIndexPath: indexPath) as! ValueCollectionViewCell
cell.ValueCollectionViewLabel.text = BluetoothManager.bluetoothManager.Value[indexPath.row]
return cell
}
}

我必须在我的 BluetoothManager 类中添加什么才能重新加载 CollectionView?我想到了类似的东西:ValueCollectionView.reloadData() 但我不知道如何实现它。谢谢!

最佳答案

试试这个:

import UIKit
import CoreBluetooth

class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
static var bluetoothManager = BluetoothManager()
...
var ValueCollectionView: UICollectionView?
var Value: [String] = [""]
...
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?){
//For this example, there is only a string value to update
Value[0] = "It works!"
ValueCollectionView?.reloadData()
}}

import UIKit

class ValueCollectionView: UICollectionViewController{
@IBOutlet var ValueCollectionView: UICollectionView!

var valueCollectionViewCell: ValueCollectionViewCell = ValueCollectionViewCell()

override func viewDidLoad()
{
super.viewDidLoad()
BluetoothManager.bluetoothManager.ValueCollectionView = ValueCollectionView
}

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

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return BluetoothManager.bluetoothManager.Value.count
}

override func collectionView(tableView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell: ValueCollectionViewCell = collectionView?.dequeueReusableCellWithReuseIdentifier("valueCell", forIndexPath: indexPath) as! ValueCollectionViewCell
cell.ValueCollectionViewLabel.text = BluetoothManager.bluetoothManager.Value[indexPath.row]
return cell
}
}

关于ios - 如何从 Swift 中的另一个类重新加载 Collection View Cells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33323494/

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