如何将 fetchRequest 更改为数组以在 UITableView 中使用
How do you change a fetchRequest to an Array to use in a UITableView
我想把从核心数据中获取的数据放在 UITableView
中,但我得到了这个 "EXC_BAD_INSTRUCTION" 。
使用 let swiftBlogs Array 工作得很好,所以有人可以告诉我如何将 fetch 转换为 Array 或者这不是正确的方法吗?
import UIKit
import CoreData
class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var scrollView: UIScrollView!
@IBOutlet var timeStampTextField: UITextField!
@IBOutlet var quickQuoteTextField: UITextField!
@IBOutlet var tableViewQuickQuote: UITableView!
let swiftBlogs = ["Ray Wenderlich", "NSHipster", "iOS Developer Tips", "Jameson Quave", "Natasha The Robot", "Coding Explorer", "That Thing In Swift", "Andrew Bancroft", "iAchieved.it", "Airspeed Velocity"]
var tableViewCellArray : Array<AnyObject> = []
var quickQuoteArray : Array<AnyObject> = []
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context:NSManagedObjectContext = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "QuickQuote" )
request.returnsObjectsAsFaults = false
tableViewCellArray = context.executeFetchRequest(request, error: nil)!
}
override func viewWillAppear(animated: Bool) {
quickQuoteTextField.text = ""
timeStampTextField.text = ""
}
@IBAction func clearButton(sender: AnyObject) {
quickQuoteTextField.text = ""
timeStampTextField.text = ""
}
@IBAction func addToQuickQuoteButton(sender: AnyObject) {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!
let ent = NSEntityDescription.entityForName("QuickQuote", inManagedObjectContext: context)
var newQuickQuote = QuickQuote(entity: ent!, insertIntoManagedObjectContext: context)
newQuickQuote.quickQuote = quickQuoteTextField.text
context.save(nil)
}
@IBAction func timeStampButton(sender: AnyObject) {
timeStamp()
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!
let ent = NSEntityDescription.entityForName("Time", inManagedObjectContext: context)
var newTime = Time(entity: ent!, insertIntoManagedObjectContext: context)
newTime.time = timeStampTextField.text
newTime.quote = quickQuoteTextField.text
context.save(nil)
}
func timeStamp (){
timeStampTextField.text = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: NSDateFormatterStyle.FullStyle,
timeStyle: NSDateFormatterStyle.ShortStyle)
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return swiftBlogs.count // return quickQuoteArray.count
}
private let stampCellID: NSString = "cell" //This is the cell itself's identifier.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(stampCellID as String, forIndexPath: indexPath) as! UITableViewCell
var data: NSManagedObject = quickQuoteArray[indexPath.row] as! NSManagedObject
cell.textLabel?.text = data.valueForKey("quickQuote") as? String
// let row = indexPath.row
// cell.textLabel?.text = swiftBlogs[row]
return cell
}
/*
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context:NSManagedObjectContext = appDel.managedObjectContext!
if editingStyle == UITableViewCellEditingStyle.Delete {
let tv = tableView
context.deleteObject(quickQuoteArray.self[indexPath.row] as! NSManagedObject)
tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
context.save(nil)
}
*/
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
您混淆了数组 swiftBlogs 和 quickQuoteArray。 table 视图是否尝试访问数组元素 quickQuoteArray[indexpath.row]
取决于它是否认为索引已填充,基于 numberOfRowsInSection
的结果。在 numberOfRowsInSection
方法中,您将返回 swiftBlogs 的计数,它始终是您手动输入的 10 个左右的字符串。因此,在您的请求甚至被执行之前,或者视图甚至有机会填充任何内容之前否则,它会尝试显示您在 cellForRowAtIndexPath
.
中使用的数组中不存在的元素
简而言之:
始终在 cellForRowAtIndexPath
中使用与在 numberOfRowsInSection
中使用的数组相同的数组。在这里,您混合了两个不同的数组,quickQuoteArray 和 swiftBlogs。
我想把从核心数据中获取的数据放在 UITableView
中,但我得到了这个 "EXC_BAD_INSTRUCTION" 。
使用 let swiftBlogs Array 工作得很好,所以有人可以告诉我如何将 fetch 转换为 Array 或者这不是正确的方法吗?
import UIKit
import CoreData
class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var scrollView: UIScrollView!
@IBOutlet var timeStampTextField: UITextField!
@IBOutlet var quickQuoteTextField: UITextField!
@IBOutlet var tableViewQuickQuote: UITableView!
let swiftBlogs = ["Ray Wenderlich", "NSHipster", "iOS Developer Tips", "Jameson Quave", "Natasha The Robot", "Coding Explorer", "That Thing In Swift", "Andrew Bancroft", "iAchieved.it", "Airspeed Velocity"]
var tableViewCellArray : Array<AnyObject> = []
var quickQuoteArray : Array<AnyObject> = []
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context:NSManagedObjectContext = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "QuickQuote" )
request.returnsObjectsAsFaults = false
tableViewCellArray = context.executeFetchRequest(request, error: nil)!
}
override func viewWillAppear(animated: Bool) {
quickQuoteTextField.text = ""
timeStampTextField.text = ""
}
@IBAction func clearButton(sender: AnyObject) {
quickQuoteTextField.text = ""
timeStampTextField.text = ""
}
@IBAction func addToQuickQuoteButton(sender: AnyObject) {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!
let ent = NSEntityDescription.entityForName("QuickQuote", inManagedObjectContext: context)
var newQuickQuote = QuickQuote(entity: ent!, insertIntoManagedObjectContext: context)
newQuickQuote.quickQuote = quickQuoteTextField.text
context.save(nil)
}
@IBAction func timeStampButton(sender: AnyObject) {
timeStamp()
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!
let ent = NSEntityDescription.entityForName("Time", inManagedObjectContext: context)
var newTime = Time(entity: ent!, insertIntoManagedObjectContext: context)
newTime.time = timeStampTextField.text
newTime.quote = quickQuoteTextField.text
context.save(nil)
}
func timeStamp (){
timeStampTextField.text = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: NSDateFormatterStyle.FullStyle,
timeStyle: NSDateFormatterStyle.ShortStyle)
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return swiftBlogs.count // return quickQuoteArray.count
}
private let stampCellID: NSString = "cell" //This is the cell itself's identifier.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(stampCellID as String, forIndexPath: indexPath) as! UITableViewCell
var data: NSManagedObject = quickQuoteArray[indexPath.row] as! NSManagedObject
cell.textLabel?.text = data.valueForKey("quickQuote") as? String
// let row = indexPath.row
// cell.textLabel?.text = swiftBlogs[row]
return cell
}
/*
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
var appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context:NSManagedObjectContext = appDel.managedObjectContext!
if editingStyle == UITableViewCellEditingStyle.Delete {
let tv = tableView
context.deleteObject(quickQuoteArray.self[indexPath.row] as! NSManagedObject)
tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
context.save(nil)
}
*/
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
您混淆了数组 swiftBlogs 和 quickQuoteArray。 table 视图是否尝试访问数组元素 quickQuoteArray[indexpath.row]
取决于它是否认为索引已填充,基于 numberOfRowsInSection
的结果。在 numberOfRowsInSection
方法中,您将返回 swiftBlogs 的计数,它始终是您手动输入的 10 个左右的字符串。因此,在您的请求甚至被执行之前,或者视图甚至有机会填充任何内容之前否则,它会尝试显示您在 cellForRowAtIndexPath
.
简而言之:
始终在 cellForRowAtIndexPath
中使用与在 numberOfRowsInSection
中使用的数组相同的数组。在这里,您混合了两个不同的数组,quickQuoteArray 和 swiftBlogs。