gpt4 book ai didi

swift - tableView.reloadData() 导致应用程序崩溃

转载 作者:行者123 更新时间:2023-11-28 09:59:31 26 4
gpt4 key购买 nike

在 Swift 应用程序中,我提供了一个添加 View ,用于将元素添加到 CoreData 数据库。如果我调用 tableview.reloadData(),应用程序会在主屏幕上的 + 按钮上崩溃。如果我省略重新加载数据,则会显示添加 View 并将数据添加到 CoreData 文件。

主视图,从configureCell向下:

func configureCell(cell: TransectTableViewCell, indexPath:NSIndexPath) {

let transectEntry = fetchedResultController.objectAtIndexPath(indexPath) as! Transects

cell.transectNameLabel.text = transectEntry.transectName
cell.transectNameLabel.textColor = UIColor.blackColor()
cell.transectNameLabel.shadowColor = UIColor.whiteColor()
cell.transectNameLabel.shadowOffset = CGSizeMake(1, 1)
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let countEntry = fetchedResultController.objectAtIndexPath(indexPath) as! Transects
coreDataStack.context.deleteObject(countEntry)
coreDataStack.saveContext()
}
}

func tableView(tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath)
-> CGFloat {
return 50;
}

func didFinishViewController(viewController: AddTransectViewController, didSave: Bool) {

if didSave {
var error: NSError? = nil
let context = viewController.context
self.coreDataStack.saveContext()
}

dismissViewControllerAnimated(true, completion: {})
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "addTransectSegue" {

let newTransectViewController = segue.destinationViewController as! AddTransectViewController
let transectEntryEntity = NSEntityDescription.entityForName("Transects", inManagedObjectContext: coreDataStack.context)
let newTransectEntry = Transects(entity: transectEntryEntity!, insertIntoManagedObjectContext: coreDataStack.context)

newTransectViewController.transectNewEntry = newTransectEntry
newTransectViewController.context = newTransectEntry.managedObjectContext
newTransectViewController.delegate = self
}

if segue.identifier == "transectTasksSegue" {

let indexPath = tableView.indexPathForSelectedRow()!
let transectSelected = fetchedResultController.objectAtIndexPath(indexPath) as! Transects

let tasksViewController = segue.destinationViewController as! TransectTasksViewController
tasksViewController.coreDataStack = coreDataStack

tasksViewController.selectedTransect = transectSelected

}
}


func controllerDidChangeContent(controller:
NSFetchedResultsController) {
tableView.reloadData()
}

加法 View 是:

import UIKit
import CoreData
import Foundation

protocol TransectDelegate {

func didFinishViewController(ViewController:AddTransectViewController, didSave:Bool)
}

class AddTransectViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var transectNameTextField: UITextField!
@IBOutlet weak var latitudeTextField: UITextField!
@IBOutlet weak var longitudeTextField: UITextField!
@IBOutlet weak var altitudeTextField: UITextField!

var transectNewEntry: Transects!

var context: NSManagedObjectContext!
var delegate:TransectDelegate?

override func viewDidLoad() {
super.viewDidLoad()
}

override func prefersStatusBarHidden() -> Bool {
return true
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func updateTransectEntry() {

if let entry = transectNewEntry {

entry.transectName = transectNameTextField.text
entry.latitude = latitudeTextField.text
entry.longitude = longitudeTextField.text
entry.altitude = altitudeTextField.text
}
}

@IBAction func cancelButtonWasTapped(sender: AnyObject) {
delegate?.didFinishViewController(self, didSave: false)
}

@IBAction func saveButtonWasTapped(sender: AnyObject) {
updateTransectEntry()
delegate?.didFinishViewController(self, didSave: true)
}

}

我错过了什么,但看不到什么。欢迎提出想法。

应用在 cell.transectNameLabel.text = transectEntry.transectName 上挂起

与:线程 1:EXC_BAD_ACCESS(代码=1,地址=0x0)

我真正的困惑是这完美地工作:

 import UIKit

导入核心数据

PlantSpeciesViewController 类:UIViewController、NSFetchedResultsControllerDelegate、PlantSpeciesDelegate、UITableViewDataSource、UITableViewDelegate {

@IBOutlet var tableView:UITableView!

var coreDataStack: CoreDataStack!
lazy var fetchedResultController:
NSFetchedResultsController = self.plantSpeciesFetchedResultsController()

var plantSpecies: PlantSpecies!

var selectedFamily: PlantFamily!

var context: NSManagedObjectContext!

var plantFamilyName: String!

override func viewDidLoad() {
super.viewDidLoad()

tableView.backgroundColor = UIColor.clearColor()
view.backgroundColor = UIColor(patternImage: UIImage (named: "Monitor backdrop.png")!)

self.title = plantFamilyName
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func plantSpeciesFetchedResultsController()
->NSFetchedResultsController {

fetchedResultController =
NSFetchedResultsController(
fetchRequest: plantSpeciesFetchRequest(),
managedObjectContext: coreDataStack.context,
sectionNameKeyPath: nil,
cacheName: nil)

fetchedResultController.delegate = self

var error: NSError? = nil
if (!fetchedResultController.performFetch(&error)){
println("Error: \(error?.localizedDescription)")
abort()
}

return fetchedResultController
}

func plantSpeciesFetchRequest() -> NSFetchRequest {

let fetchRequest = NSFetchRequest(entityName: "PlantSpecies")
fetchRequest.fetchBatchSize = 20

let predicate = NSPredicate(format: "familyName == %@", selectedFamily)

fetchRequest.predicate = predicate

let sortDescriptor = NSSortDescriptor(key: "plantSpecies", ascending: true)

fetchRequest.sortDescriptors = [sortDescriptor]

//var error: NSError?

return fetchRequest
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return fetchedResultController.sections!.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return fetchedResultController.sections![section].numberOfObjects
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("plantSpeciesCell", forIndexPath: indexPath) as! PlantSpeciesTableViewCell

cell.backgroundColor = UIColor.clearColor()

configureCell(cell, indexPath: indexPath)

return cell
}

func configureCell(cell: PlantSpeciesTableViewCell, indexPath:NSIndexPath) {

let plantEntry = fetchedResultController.objectAtIndexPath(indexPath) as! PlantSpecies

cell.speciesNameLabel.text = plantEntry.plantSpecies
cell.speciesNameLabel.textColor = UIColor.blackColor()
cell.speciesNameLabel.shadowColor = UIColor.whiteColor()
cell.speciesNameLabel.shadowOffset = CGSizeMake(1, 1)

cell.speciesImageView.image = UIImage (data: plantEntry.plantImage)
}


func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let countEntry = fetchedResultController.objectAtIndexPath(indexPath) as! PlantFamily
coreDataStack.context.deleteObject(countEntry)
coreDataStack.saveContext()
}
}

func tableView(tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath)
-> CGFloat {
return 90;
}

func didFinishViewController(viewController: AddPlantSpeciesViewController, didSave: Bool) {

if didSave {
var error: NSError? = nil
let context = viewController.context
self.coreDataStack.saveContext()
}

dismissViewControllerAnimated(true, completion: {})
}

// MARK: - Navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "addSpeciesSegue" {

let newPlantViewController = segue.destinationViewController as! AddPlantSpeciesViewController
let plantEntryEntity = NSEntityDescription.entityForName("PlantSpecies", inManagedObjectContext: coreDataStack.context)
let newSpeciesEntry = PlantSpecies(entity: plantEntryEntity!, insertIntoManagedObjectContext: coreDataStack.context)

newPlantViewController.selectedFamily = selectedFamily

newPlantViewController.plantNameEntry = newSpeciesEntry
newPlantViewController.context = newSpeciesEntry.managedObjectContext
newPlantViewController.delegate = self
}
}

func controllerDidChangeContent(controller:
NSFetchedResultsController) {

tableView.reloadData()
}

加上:

导入 UIKit导入核心数据导入基金会

协议(protocol) PlantSpeciesDelegate {

func didFinishViewController(ViewController:AddPlantSpeciesViewController, didSave:Bool)

类 AddPlantSpeciesViewController:UIViewController、UITextFieldDelegate、UIImagePickerControllerDelegate、UINavigationControllerDelegate {

@IBOutlet weak var plantNameTextField: UITextField!
@IBOutlet weak var plantImageView: UIImageView!

@IBOutlet weak var imageSwitch: UISwitch!

@IBOutlet weak var imageFromFileButton: UIButton!
@IBOutlet weak var imageFromCameraButton: UIButton!

let imagePicker = UIImagePickerController()

var plantNameEntry: PlantSpecies!
var selectedFamily: PlantFamily!

var passedPlantFamily: String!

var newPlantName: String!
var newImageData: NSData!

var context: NSManagedObjectContext!
var delegate:PlantSpeciesDelegate?

override func viewDidLoad() {
super.viewDidLoad()

plantImageView.image = UIImage(named: "placeholder image.jpg")

imagePicker.delegate = self

}

override func prefersStatusBarHidden() -> Bool {
return true
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func imageSourceSwitch(sender: AnyObject) {

if imageSwitch.on == true
{
self.imageFromFileButton.enabled = true
self.imageFromCameraButton.enabled = false
}

else
{
self.imageFromCameraButton.enabled = true
self.imageFromFileButton.enabled = false
}
}


@IBAction func imageFromFile(sender: AnyObject) {
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}


@IBAction func imageFromCamera(sender: AnyObject) {
imagePicker.sourceType = .Camera
presentViewController(imagePicker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.plantImageView.image = image
dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func getPlantName() {

newPlantName = plantNameTextField.text
plantNameTextField.resignFirstResponder()
}

func updateSpeciesEntry() {

if let entry = plantNameEntry {

entry.plantSpecies = newPlantName
entry.plantImage = UIImageJPEGRepresentation(plantImageView.image, 1.0)
entry.familyName = selectedFamily
}
}

@IBAction func cancelButtonWasTapped(sender: AnyObject) {
delegate?.didFinishViewController(self, didSave: false)
}


@IBAction func saveButtonWasTapped(sender: AnyObject) {
updateSpeciesEntry()
delegate?.didFinishViewController(self, didSave: true)
}

} 那么,有什么区别呢?

最佳答案

这也发生在我身上,修复它:

dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});

不仅仅是 tableView.reloadData(),因为它似乎是从错误的线程调用的。

关于swift - tableView.reloadData() 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30988736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com