我在接收一些BLE数据时尝试重新加载CollectionView。这是我的蓝牙类:
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数组的第一项是字符串"它工作"。现在我想刷新/重新加载我的CollectionView控制器,以显示新值。这是我的Collection视图类:
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()
这样的东西,但我不知道如何实现它。谢谢!
答案 0 :(得分:2)
试试这个:
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
}
}