使用 NSFetchResultsController 时 TableView 不显示任何内容
TableView not show anything when using NSFetchResultsController
我尝试制作像商店这样的小应用程序,我将对象内容名称和图像保存在 coreData 中,并尝试使用 NSFetchResultsController 调用它,但它没有告诉我任何想法
我在 http://www.mediafire.com/file/kyb06frcmm7boqa/MyStore.zip/file
上上传了我的项目
我没有收到任何错误,但在 tableview 中没有显示任何想法我尝试了所有解决方法,但它不起作用
这是ViewController代码
class ViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate,NSFetchedResultsControllerDelegate {
@IBOutlet weak var LaTableView: UITableView!
var controller:NSFetchedResultsController<Item>!
override func viewDidLoad() {
super.viewDidLoad()
LaTableView.dataSource=self
LaTableView.delegate=self
loadIetms()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sections = controller.sections{
let secinfo = sections[section]
return secinfo.numberOfObjects }
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "itemcell", for: indexPath) as! TableViewCell
ConCell(cell: cell, indexPath: indexPath)
return cell
}
func ConCell(cell:TableViewCell , indexPath:IndexPath){
let itemu = controller.object(at: indexPath)
cell.SetCellContent(item: itemu)
}
// End OF Table View
// Load From CoreData
func loadIetms(){
let requstfetch:NSFetchRequest<Item>=Item.fetchRequest()
let date_add = NSSortDescriptor(key: "name_item", ascending: false)
requstfetch.sortDescriptors=[date_add]
controller = NSFetchedResultsController(fetchRequest: requstfetch, managedObjectContext: Context, sectionNameKeyPath: nil, cacheName: nil)
controller.delegate = self
do{
try controller.performFetch()
}
catch{
print("can't load data")
}}
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>){
LaTableView.beginUpdates()
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>){
LaTableView.endUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch (type) {
case.insert:
if let indexPath = newIndexPath {
LaTableView.insertRows(at: [indexPath], with: .fade)
}
break
case.delete:
if let indexPath = indexPath {
LaTableView.deleteRows(at: [indexPath], with: .fade)
}
break
case.update:
if let indexPath = indexPath {
let cell = LaTableView.cellForRow(at: indexPath) as! TableViewCell
ConCell(cell: cell, indexPath: indexPath)
}
break
case.move:
if let indexPath = indexPath {
LaTableView.deleteRows(at: [indexPath], with: .fade)
}
if let indexPath = newIndexPath {
LaTableView.insertRows(at: [indexPath], with: .fade)
}
break
}
}
}
这是 TableViewCell 的代码
class TableViewCell: UITableViewCell {
@IBOutlet weak var LaImageView: UIImageView!
@IBOutlet weak var LaItemName: UILabel!
@IBOutlet weak var LaStoreName: UILabel!
func SetCellContent(item:Item){
LaItemName.text = item.name_item
LaImageView.image = item.image as? UIImage
LaStoreName.text = item.toStore?.name
问题已通过使用 main.stroyborad
中的约束修复 TableView 解决
我尝试制作像商店这样的小应用程序,我将对象内容名称和图像保存在 coreData 中,并尝试使用 NSFetchResultsController 调用它,但它没有告诉我任何想法
我在 http://www.mediafire.com/file/kyb06frcmm7boqa/MyStore.zip/file
上上传了我的项目我没有收到任何错误,但在 tableview 中没有显示任何想法我尝试了所有解决方法,但它不起作用
这是ViewController代码
class ViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate,NSFetchedResultsControllerDelegate {
@IBOutlet weak var LaTableView: UITableView!
var controller:NSFetchedResultsController<Item>!
override func viewDidLoad() {
super.viewDidLoad()
LaTableView.dataSource=self
LaTableView.delegate=self
loadIetms()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sections = controller.sections{
let secinfo = sections[section]
return secinfo.numberOfObjects }
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "itemcell", for: indexPath) as! TableViewCell
ConCell(cell: cell, indexPath: indexPath)
return cell
}
func ConCell(cell:TableViewCell , indexPath:IndexPath){
let itemu = controller.object(at: indexPath)
cell.SetCellContent(item: itemu)
}
// End OF Table View
// Load From CoreData
func loadIetms(){
let requstfetch:NSFetchRequest<Item>=Item.fetchRequest()
let date_add = NSSortDescriptor(key: "name_item", ascending: false)
requstfetch.sortDescriptors=[date_add]
controller = NSFetchedResultsController(fetchRequest: requstfetch, managedObjectContext: Context, sectionNameKeyPath: nil, cacheName: nil)
controller.delegate = self
do{
try controller.performFetch()
}
catch{
print("can't load data")
}}
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>){
LaTableView.beginUpdates()
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>){
LaTableView.endUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch (type) {
case.insert:
if let indexPath = newIndexPath {
LaTableView.insertRows(at: [indexPath], with: .fade)
}
break
case.delete:
if let indexPath = indexPath {
LaTableView.deleteRows(at: [indexPath], with: .fade)
}
break
case.update:
if let indexPath = indexPath {
let cell = LaTableView.cellForRow(at: indexPath) as! TableViewCell
ConCell(cell: cell, indexPath: indexPath)
}
break
case.move:
if let indexPath = indexPath {
LaTableView.deleteRows(at: [indexPath], with: .fade)
}
if let indexPath = newIndexPath {
LaTableView.insertRows(at: [indexPath], with: .fade)
}
break
}
}
}
这是 TableViewCell 的代码
class TableViewCell: UITableViewCell {
@IBOutlet weak var LaImageView: UIImageView!
@IBOutlet weak var LaItemName: UILabel!
@IBOutlet weak var LaStoreName: UILabel!
func SetCellContent(item:Item){
LaItemName.text = item.name_item
LaImageView.image = item.image as? UIImage
LaStoreName.text = item.toStore?.name
问题已通过使用 main.stroyborad
中的约束修复 TableView 解决