TableView / CoreData字幕和标题

时间:2016-06-21 12:54:37

标签: ios swift uitableview core-data tableview

我正在创建一个列表应用程序,并且有一个Coredata模型,其中包含两个属性(一个用于项目,一个用于详细信息)

我可以保存并输入数据,它会正确显示但是当我关闭并重新打开应用程序时,一切都有自己的单元格。

任何帮助人?

CellAtIndex代码

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell
    let item = listItems[indexPath.row]
    let detailed = detailedItems[indexPath.row]
    cell.textLabel?.text = item.valueForKey("item") as? String
    cell.detailTextLabel?.text = detailed.valueForKey("detail") as? String
    return cell
}

ViewWillAppear代码

verride func viewWillAppear(animated: Bool) {
    //Load First Item
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext

    let fetchrequest = NSFetchRequest(entityName: "ListEnt")
    do{
        let results = try managedContext.executeFetchRequest(fetchrequest)
        listItems = results as! [NSManagedObject]
        detailedItems = results as! [NSManagedObject]
    }catch{
        print("Error")
    }
}

这是保存的代码,我有一个对话框,带有文本字段,每个文本字段都有自己的保存功能

 //Saving Items//
func saveItem(itmToSave: String){
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext
    let entity = NSEntityDescription.entityForName("ListEnt", inManagedObjectContext: managedContext)
    let item = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
    item.setValue(itmToSave, forKey: "item")
    do{
        try managedContext.save()
        listItems.append(item)
    }catch{
        print("Saving Main Item")
    }

}
//Saving Details
func saveItem2(itmToSave2: String){
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext
    let entity = NSEntityDescription.entityForName("ListEnt", inManagedObjectContext: managedContext)
    let item2 = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
    item2.setValue(itmToSave2, forKey: "detail")
    do{
        try managedContext.save()
        detailedItems.append(item2)
    }catch{
        print("Saving deatil Item")
    }

}

问题图片

输入数据时的外观(正确方式)

How it looks when you input data (Correct way)

关闭并重新打开应用时的外观如何

How it looks when you close and reopen the app

干杯伙伴

0 个答案:

没有答案