作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我尝试在接收到一些 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/
我是一名优秀的程序员,十分优秀!