before write down what I want to say
my English is not good but plz give me an answer
my situation is when I edit my text and save it it works well but the nil cell is added on the table at the same time
when I add a new text then it never make a nil cell but only edit works wrong plz give me any advices or answer
here is my code
import UIKit
import CoreData
class ComposeViewController: UIViewController {
let formatter:DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "YYYY년 MM월 dd일 E요일"
formatter.locale = Locale.init(identifier: "koKR")
return formatter
}()
// var segueData: TodoList?
var editSegueData: TodoList?
var itemContent:[NSManagedObject] = []
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var textView: UITextView!
@IBAction func save( sender: AnyObject) {
guard let memo = textView.text , memo.count > 0 else{
alert(message: "텍스트를 입력하세요")
return
}
let date = Date()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "TodoList", in: context)!
let theTitle = NSManagedObject(entity: entity, insertInto: context)
let checkBoxBool = false
if let target = editSegueData
{
print("target.content = \(target.content) \(target.insertDate)")
target.content = memo
do
{
try context.save()
// itemContent.append(theTitle)
}
catch
{
print("There was a error in saving")
}
NotificationCenter.default.post(name:ComposeViewController.memoDidChange, object: nil)
}
else
{
theTitle.setValue(memo, forKey: "content")
theTitle.setValue(date, forKey: "insertDate")
theTitle.setValue(checkBoxBool, forKey: "checkBox")
do
{
try context.save()
// itemContent.append(theTitle)
}
catch
{
print("There was a error in saving")
}
}
NotificationCenter.default.post(name: ComposeViewController.newMemoDidInsert, object: nil)
dismiss(animated: true, completion: nil)
}
@IBAction func cancelButton(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
dateLabel.text = formatter.string(from: Date())
if let memo = editSegueData {
navigationItem.title = "EDIT"
textView.text = memo.content
}else{
navigationItem.title = "NEW"
textView.text = ""
}
// Do any additional setup after loading the view.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
extension ComposeViewController {
static let newMemoDidInsert = Notification.Name("newMemoDidInsert")
static let memoDidChange = Notification.Name(rawValue: "memoDidChange")
}
my English is not good but plz give me an answer
my situation is when I edit my text and save it it works well but the nil cell is added on the table at the same time
when I add a new text then it never make a nil cell but only edit works wrong plz give me any advices or answer
here is my code
import UIKit
import CoreData
class ComposeViewController: UIViewController {
let formatter:DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "YYYY년 MM월 dd일 E요일"
formatter.locale = Locale.init(identifier: "koKR")
return formatter
}()
// var segueData: TodoList?
var editSegueData: TodoList?
var itemContent:[NSManagedObject] = []
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var textView: UITextView!
@IBAction func save( sender: AnyObject) {
guard let memo = textView.text , memo.count > 0 else{
alert(message: "텍스트를 입력하세요")
return
}
let date = Date()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "TodoList", in: context)!
let theTitle = NSManagedObject(entity: entity, insertInto: context)
let checkBoxBool = false
if let target = editSegueData
{
print("target.content = \(target.content) \(target.insertDate)")
target.content = memo
do
{
try context.save()
// itemContent.append(theTitle)
}
catch
{
print("There was a error in saving")
}
NotificationCenter.default.post(name:ComposeViewController.memoDidChange, object: nil)
}
else
{
theTitle.setValue(memo, forKey: "content")
theTitle.setValue(date, forKey: "insertDate")
theTitle.setValue(checkBoxBool, forKey: "checkBox")
do
{
try context.save()
// itemContent.append(theTitle)
}
catch
{
print("There was a error in saving")
}
}
NotificationCenter.default.post(name: ComposeViewController.newMemoDidInsert, object: nil)
dismiss(animated: true, completion: nil)
}
@IBAction func cancelButton(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
dateLabel.text = formatter.string(from: Date())
if let memo = editSegueData {
navigationItem.title = "EDIT"
textView.text = memo.content
}else{
navigationItem.title = "NEW"
textView.text = ""
}
// Do any additional setup after loading the view.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
extension ComposeViewController {
static let newMemoDidInsert = Notification.Name("newMemoDidInsert")
static let memoDidChange = Notification.Name(rawValue: "memoDidChange")
}
Could you first show exactly all the print messages you get, in their exact order.line 45 works wrong
editing is working well but when I tap the save button, save one more cell in the core data
line 53 is only for a new cell not for editing line 45 is for editing if I can put a photo here then I can explain well
If you can post a photo, that may help.