我正在寻找解决方案,以避免在我的代码中使用NSNotifications。这是我的故事板的摘录,用于解释我的问题:
一般来说,涉及两个类别:
ScanTableView
\ ValueCollectionView
//班级BluetoothManager
对我的问题并不感兴趣,而且运作正常。
ScanTableView类:
import UIKit
class ScanTableView: UITableViewController
{
@IBOutlet var ScanTableView: UITableView!
var valueCollectionView: ValueCollectionView?
override func viewDidLoad()
{
super.viewDidLoad()
self.refreshControl!.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return BluetoothManager.bluetoothManager.peripheralArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = ScanTableView.dequeueReusableCellWithIdentifier("scanCell",forIndexPath: indexPath)
cell.textLabel!.text = BluetoothManager.bluetoothManager.peripheralArray[indexPath.row].name
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
BluetoothManager.bluetoothManager.connectPeripheral(indexPath.row)
}
func refresh()
{
BluetoothManager.bluetoothManager = BluetoothManager()
}
}
ValueCollectionView类:
import UIKit
class ValueCollectionView: UICollectionViewController
{
@IBOutlet var ValueCollectionView: UICollectionView!
var scanTableView: ScanTableView?
var valueCollectionViewCell: ValueCollectionViewCell = ValueCollectionViewCell()
override func viewDidLoad()
{
super.viewDidLoad()
BluetoothManager.bluetoothManager.valueCollectionView = ValueCollectionView
self.navigationItem.hidesBackButton = true
let disconnectButton = UIBarButtonItem(title: "Disconnect", style: UIBarButtonItemStyle.Plain, target: self, action: "disconnect:")
self.navigationItem.leftBarButtonItem = disconnectButton;
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
//Function to go back to the ScanTableView
//The ScanTableView should be clear after the reload
func disconnect(sender: UIBarButtonItem)
{
BluetoothManager.bluetoothManager.disconnectPeripheral(BluetoothManager.bluetoothManager.selectedPeripheralIndex!)
//Remove all Items of the TableView
BluetoothManager.bluetoothManager.peripheralArray.removeAll()
let switchViewController = self.navigationController?.viewControllers[1] as! ScanTableView
self.navigationController?.popToViewController(switchViewController, animated: true)
// This part doesn't work! The appearing TableView isn't cleared
scanTableView?.ScanTableView.reloadData()
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return BluetoothManager.bluetoothManager.measurementValue.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.measurementValue[indexPath.row]
return cell
}
}
问题:
为什么我无法在标记的部分重新加载我的ScanTableView?通话结束后没有任何事情发生:
scanTableView?.ScanTableView.reloadData()
答案 0 :(得分:1)
在您的代码中,您没有将任何对象设置为ValueCollectionView的scanTableView
。因此,它是nil
,而reloadData()
行无效。
换句话说,如果你想使用委托,那么必须 委托。