我有一个UIViewController
,其中嵌入了一个UITableView
。
在ViewController上,我创建了:
@IBOutlet var tableView: UITableView!
然后我将其拖动到tableView
中,以将其链接到IB中。
然后,我将委托和协议链接回ViewController
。
但错误消息“ 对成员‘TableView(_:numberOfRowsSection)的引用不正确”继续出现。
我认为解决方案应该很简单,但我确实无法解决。 出了什么问题?
代码:
import UIKit
import CoreData
class BG006bViewControllerMain: UIViewController {
@IBOutlet var tableView: UITableView!
注意:numberOfSections,numberOfRowsInSection,cellForRowAt-所有这些都没问题。
import UIKit
import CoreData
class BG006bTableViewController: UIViewController,UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {
var BG006b: [BGTest006bClass] = []
var fetchResultController: NSFetchedResultsController<BGTest006bClass>!
override func viewDidLoad() {
// super.viewDidLoad()
// 2) Declare an instance variable for fetch resule controller
// let fetchRequest: NSFetchRequest<BGTest006bClass> = BG006b.fetchRequest()
let fetchRequest: NSFetchRequest<BGTest006bClass> = BGTest006bClass.fetchRequest()
// 3) Deploy Standdard codes
let sortDescriptor = NSSortDescriptor(key: "name1", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
let context = appDelegate.persistentContainer.viewContext
//Initialize the fetchResultController
fetchResultController = NSFetchedResultsController(
fetchRequest: fetchRequest,
managedObjectContext: context,
sectionNameKeyPath:nil, cacheName: nil)
fetchResultController.delegate = self
do{
try fetchResultController.performFetch()
if let fetchedObjects = fetchResultController.fetchedObjects{
BG006b = fetchedObjects
}
} catch {
print(error)
}
}
}
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return BG006b.count
}
func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell { //让单元格= tableView.dequeueReusableCell(withIdentifier:“ reuseIdentifier”,用于:indexPath)
let cellIdentifier = "BGTest006b"
//let cell = tableView.dequeuReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CustomViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! BG006bTableViewCell
cell.name1_Label.text = BG006b[indexPath.row].name1
cell.name2_Label.text = BG006b[indexPath.row].name2
cell.name3_Label.text = BG006b[indexPath.row].name3
return cell
}
问题从这里开始:
// MARK: - NSFetchedResultsControllerDelegate methods
import UIKit
import CoreData
class BG006bTableViewController: UIViewController,UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>){
tableView.beginUpdates()
} **error - Ambiguous.......**
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?){
switch type {
case .insert:
if let newIndexPath = newIndexPath {
tableView.insertRows(at: [newIndexPath], with: .fade) **error - Ambiguous.......**
}
case .delete:
if let indexPath = indexPath {
tableView.deleteRows(at: [indexPath], with: .fade)
} **error - Ambiguous.......**
case .update:
if let indexPath = indexPath {
tableView.reloadRows(at: [indexPath], with: .fade)
} **error - Ambiguous.......**
default:
tableView.reloadData()
} **error - Ambiguous.......**
if let fetchedObjects = controller.fetchedObjects{
BG006b = fetchedObjects as! [BGTest006bClass]
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
} **error - Ambiguous.......**