如何从Swift中的另一个类重新加载CollectionViewCells

时间:2015-10-24 21:21:43

标签: ios swift uicollectionview

我在接收一些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()这样的东西,但我不知道如何实现它。谢谢!

1 个答案:

答案 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
    }
}
相关问题