微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

快速,核心数据值更新问题,何时更新值,然后也添加了一个空单元格

如何解决快速,核心数据值更新问题,何时更新值,然后也添加了一个空单元格

我尝试更新核心数据中的值,但是当我更新值时,也使用正确的更新单元格enter image description here

在tableView上添加了零单元格

这是我撰写View控制器源代码的一部分

    @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
        {
            target.content = memo
//            DataManager.shared.saveContext()
            if context.hasChanges
            {
                print("in")
                do
                {
                    try context.save()
                }
                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()
            }
            catch
            {
                print("There was a error in saving")
            }
             NotificationCenter.default.post(name: ComposeViewController.newMemoDidInsert,object: nil)
        }
        
       
        

        dismiss(animated: true,completion: nil)
    }

这是我的主表视图源代码的一部分

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        var nav = self.navigationController?.navigationBar
        nav?.barStyle = UIBarStyle.black
        nav?.tintColor = UIColor.white
        nav?.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.gray]
        refreshControl.endRefreshing()
        
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext
        let fetchRequest = NSFetchRequest<NSManagedobject>(entityName: "TodoList")
        do{
            itemContent = try context.fetch(fetchRequest)
        }catch{
            print("Error in loading data")
        }
        itemContent.reverse()
        
        
        mainTableView.reloadData()
        
    }
    


    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController!.navigationBar.largeTitleTextAttributes = [.font: UIFont.systemFont(ofSize: 25),NSAttributedString.Key.foregroundColor:UIColor.gray]
        searchBar.delegate = self
        mainTableView.tableFooterView = UIView()
        mainTableView.backgroundColor = .clear
        //-------------------------------------------
        mainTableView.addSubview(refreshControl)
        
        self.searchBar.layer.borderColor = UIColor.white.cgColor
        self.searchBar.layer.borderWidth = 1
        self.searchBar.layer.cornerRadius = 5.0
        self.searchBar.clipsToBounds = true
        
        var gestureRec = UILongPressGestureRecognizer(target: self,action: #selector(handleTap))
        self.mainTableView.addGestureRecognizer(gestureRec)
        
        token = NotificationCenter.default.addobserver(forName: ComposeViewController.memoDidChange,object: nil,queue: OperationQueue.main,using: { [weak self] (noti) in
            self?.mainTableView.reloadData()
        })
        
    }

这里是发送值以构成View Controller进行编辑的部分

if sender.state == .ended {
                var location = sender.location(in: self.mainTableView)
                var indexPath = self.mainTableView.indexPathForRow(at: location)
                var cell = self.mainTableView.cellForRow(at: indexPath!)
                
                if let controller = self.storyboard?.instantiateViewController(withIdentifier: "ComposeViewNavigationController") as? UINavigationController,let yourViewController = controller.viewControllers.first as? ComposeViewController {
                        yourViewController.editSegueData = itemContent[indexPath!.row] as? TodoList

                    present(controller,animated: true,completion: nil)
                }

总结一下,当我调用composeView进行编辑并且更新值时,更新效果很好,但同时添加一个空单元格 所以请给我任何建议...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。