gpt4 book ai didi

ios - 填充 TableView 中的 JSON 数组和 CoreData

转载 作者:行者123 更新时间:2023-11-30 14:15:14 25 4
gpt4 key购买 nike

抱歉给您带来不便,我是 iOS 初学者,遇到问题...

我有一个 JSON、CoreData 和 TableView,JSON 从网络获取并保存在文档文件夹中。之后,必须用它填充 CoreData 和 TableView 中保存的 JSON 中的一些值。 F.E. 我们采用 event_title..

我有一个 Controller :

import Foundation
import UIKit
import CoreLocation
import CoreData

class QuestListViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {

let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

let eventscd = [Events]()

@IBOutlet weak var questsTabBarItem: UITabBarItem!

var questCode : String = ""

var numberofcells:Int = 0

@IBOutlet weak var tableView: UITableView!

var numbers:[String] = []

override func viewDidLoad() {
super.viewDidLoad()

let entity = NSEntityDescription.entityForName("Events", inManagedObjectContext: managedObjectContext!)

let events = Events(entity: entity!, insertIntoManagedObjectContext: managedObjectContext)

var error: NSError?
var jsonError:NSError?
let jsonBundle = NSBundle.mainBundle()
let jsonFolder = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]as! String
let jsonPath = jsonFolder.stringByAppendingPathComponent("\(self.questCode).json")

if let data = NSData(contentsOfFile:jsonPath, options:NSDataReadingOptions.DataReadingUncached, error:&jsonError) {

let stringData = NSString(data: data, encoding: NSUTF8StringEncoding)

let jsonDict : NSDictionary? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonError) as? NSDictionary

if let err = jsonError {
println("error parsing json")
}

if let jsonDictionary = jsonDict {
println("json parsed successfully")

let json_data = JSON(jsonDictionary).arrayValue

let id = jsonDictionary["id"]!.stringValue
let version: AnyObject = jsonDictionary["version"]!
let title: AnyObject = jsonDictionary["title"]!
let description: AnyObject = jsonDictionary["description"]!
let events_json = jsonDictionary["events"] as! NSArray
let eventNumber: NSNumber = jsonDictionary["events"]!.count as NSNumber
self.numberofcells = events_json.count

}

}


// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)

let row = indexPath.row

}

// MARK: - Set number of rows
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.numberofcells
}

// MARK: - Create cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.backgroundColor = UIColor.clearColor()
cell.backgroundView = UIView.new()
cell.selectedBackgroundView = UIView.new()

var imageView = UIImageView(frame: CGRectMake(10, 10, cell.frame.width - 0.3, cell.frame.height - 0.3))
let image = UIImage(named: "event_list_cell.png")
imageView.image = image
cell.backgroundView = UIView()
cell.backgroundView!.addSubview(imageView)
var row = indexPath.row
let eventsc = eventscd[indexPath.row] /////HERE IS FATAL BREAKPOINT

// cell.textLabel?.text = eventTable[row] as? String
cell.textLabel?.text = eventsc.valueForKey("event_title")![row] as? String

return cell
}

}

事件类: 进口基金会 导入核心数据

@objc(Events) class Events: NSManagedObject {

@NSManaged var title: String
@NSManaged var event_title: String

}

还有一个 CoreMap: http://i.imgur.com/5iKwAyn.png

并且有一个错误:

On line with "let eventsc = eventscd[indexPath.row]" in Controller
Output: "fatal error: Array index out of range"

我哪里错了?

最佳答案

这是因为在 viewDidLoad 中,您将 numberOfCells 设置为 > 0 的值。但是 eventscd 中没有任何内容。这就是为什么当尝试从 eventscd 读取值时,您会收到索引超出范围错误。

理想情况下,返回的行数应为 eventscd.count,并且应在加载表内容之前填充 eventscd(或者在获取数据并添加值后重新加载表)到 eventscd)

关于ios - 填充 TableView 中的 JSON 数组和 CoreData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31270122/

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