当添加到表视图境界项目出现两次

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当添加到表视图境界项目出现两次相关的知识,希望对你有一定的参考价值。

我正在开发一个应用程序,您可以添加项目使用领域数据库中的表视图。此刻,我可以添加一个项目到表视图,但它被添加两次境界数据库。这就意味着,如果我关我的应用程序和然后返回到它,我以前增加了新的项目出现了两次,一次是在新的位置和更换一次,这是在它上面的表视图的项目。附件是我的代码。我已经尝试了各种不同的东西,但只是无法弄清楚如何解决它。谢谢您的帮助!

这是我加入的项目,以我的表视图/域数据库的代码:

extension MainTableViewController 

func createNewEventAlertView () 

    let newEventAlert = PMAlertController(title: "New Event Alert", description: "", image: nil, style: .alert)
    newEventAlert.addTextField  (eventNameAlertTextField) in
        eventNameAlertTextField?.placeholder = "Event Name"
        eventNameAlertTextField?.adjustsFontSizeToFitWidth = true
        eventNameAlertTextField?.textAlignment = .center
        eventNameAlertTextField?.text! = nameOfEvent.text!
        nameOfEvent = eventNameAlertTextField!
    
    newEventAlert.addTextField  (dayEventDateTextField) in
        dayEventDateTextField?.placeholder = "Day Of The Month"
        dayEventDateTextField?.inputView = dayPickerView
        dayOfEvent = dayEventDateTextField!
    
    newEventAlert.addTextField  (monthEventDateTextField) in
        monthEventDateTextField?.placeholder = "Month"
        monthEventDateTextField?.inputView = monthPickerView
        monthOfEvent = monthEventDateTextField!
    
    newEventAlert.addTextField  (remindOneDayBeforeTextField) in
        remindOneDayBeforeTextField?.placeholder = "Remind 1 Day Before?"
        remindOneDayBeforeTextField?.inputView = oneDayPickerView
        oneDayReminder = remindOneDayBeforeTextField!
    
    newEventAlert.addTextField  (remindSevenDaysBeforeTextField) in
        remindSevenDaysBeforeTextField?.placeholder = "Remind 7 Days Before?"
        remindSevenDaysBeforeTextField?.inputView = sevenDayPickerView
        sevenDayReminder = remindSevenDaysBeforeTextField!
    


    newEventAlert.addAction(PMAlertAction(title: "Add", style: .default, action: 
        print("Add Event Button Pressed in Alert")

        //Adding realmEventItem & its variables to the Database
        try! realm.write 
            //Adding the name & date of the Event to the Realm Database Item
            self.realmEventItem.nameOfEventRealm = self.nameOfEvent.text!
            let fullDateOfEvent = self.dayOfEvent.text! + " " + self.monthOfEvent.text!
            self.realmEventItem.dateOfEventRealm = fullDateOfEvent

            //Telling the Realm Database whether they wan't to be reminded one & seven days before the event
            if self.oneDayReminder.text == "Yes" 
                self.realmEventItem.oneDayBeforeReminderRealm = true
             else self.realmEventItem.oneDayBeforeReminderRealm = false

            if self.sevenDayReminder.text == "Yes" 
                self.realmEventItem.sevenDaysBeforeReminderRealm = true
             else self.realmEventItem.sevenDaysBeforeReminderRealm = false


            // Making Sure there is something written in the Name & Date Text Fields before allowing anything to be added
            if self.nameOfEvent.text != "" && self.dayOfEvent.text != "" && self.monthOfEvent.text != "" 
                    /*
                     - use create method instead of add method to add new entries,
                     - Because add method replaces last new entry with our previous entry,
                     - so every time only one object store in database
                     */


                realm.create(RealmEventItem.self, value: self.realmEventItem, update: false)
                self.tableView.insertRows(at: [IndexPath.init(row: realmEventList.count-1, section: 0)], with: .automatic)

                //By Adding the follwing line it has meant that my code no longer crashes when I add an Item, then delete it & then try and add another item again.
                self.tableView.numberOfRows(inSection: realmEventList.count)
                print(realmEventList.count)


                self.labelWhenTableViewIsEmpty.text = ""
                    print("Add to Table View Called")

             else 
                // Missing Boxes
                print("Missing Information!")
            


            // This makes sure that each time the user goes to add a new Event all the picker are reset
            if self.nameOfEvent.text != ""  self.nameOfEvent.text = "" 
            if self.dayPickerView.selectedRow(inComponent: 0) != 0  self.dayPickerView.selectRow(0, inComponent: 0, animated: false) 
            if self.monthPickerView.selectedRow(inComponent: 0) != 0 self.monthPickerView.selectRow(0, inComponent: 0, animated: false)
            if self.oneDayPickerView.selectedRow(inComponent: 0) != 0 self.oneDayPickerView.selectRow(0, inComponent: 0, animated: false)
            if self.sevenDayPickerView.selectedRow(inComponent: 0) != 0 self.sevenDayPickerView.selectRow(0, inComponent: 0, animated: false)
        


        print("REAL DATA:::-- \(realm.objects(RealmEventItem.self))")
    ))

    newEventAlert.addAction(PMAlertAction(title: "Cancel", style: .cancel, action: 
        print("Cancel Button Pressed in Alert")
    ))

    present(newEventAlert, animated: true, completion: nil)



答案

我已设法通过使用类型RealmEventItem()的一个新变量来解决该问题的代码改变(这是内部试realm.write!)现在是如下:

 try! realm.write 

           //The concept of adding a newEventItem of type RealmEventItem() & and then adding all the values to it rather than direct to the RealmEventItem() fixed the problem of the new items replacing the previous ones.
            let newEventItem = RealmEventItem()

            // Joining the Day & Month Together
            let fullDateOfEvent = self.dayOfEvent.text! + " " + self.monthOfEvent.text!
            // Making Variables to give a boolean of whether the user would like reminders
            var oneDayReminderItem = false
            var sevenDayReminderItem = false

            if self.oneDayReminder.text == "Yes" 
                oneDayReminderItem = true
             else oneDayReminderItem = false

            if self.sevenDayReminder.text == "Yes" 
                sevenDayReminderItem = true
             else sevenDayReminderItem = false

            //Setting the values to the newEventItem --> which is part of the RealmEventItem()
            newEventItem.nameOfEventRealm = self.nameOfEvent.text!
            newEventItem.dateOfEventRealm = fullDateOfEvent
            newEventItem.oneDayBeforeReminderRealm = oneDayReminderItem
            newEventItem.sevenDaysBeforeReminderRealm = sevenDayReminderItem


            // Making Sure there is something written in the Name & Date Text Fields before allowing anything to be added
            if self.nameOfEvent.text != "" && self.dayOfEvent.text != "" && self.monthOfEvent.text != "" 
                    /*
                     - use create method instead of add method to add new entries,
                     - Because add method replaces last new entry with our previous entry,
                     - so every time only one object store in database
                     */
                //realm.add(newEventItem, update: false)
                realm.create(RealmEventItem.self, value: newEventItem, update: false)
                self.tableView.insertRows(at: [IndexPath.init(row: realmEventList.count-1, section: 0)], with: .automatic)

                //By Adding the following line it has meant that my code no longer crashes when I add an Item, then delete it & then try and add another item again.
                self.tableView.numberOfRows(inSection: realmEventList.count)
                print(realmEventList.count)


                self.labelWhenTableViewIsEmpty.text = ""
                    print("Add to Table View Called")

             else 
                // Missing Boxes
                print("Missing Information!")
            
            

希望这可以帮助别人的未来!

以上是关于当添加到表视图境界项目出现两次的主要内容,如果未能解决你的问题,请参考以下文章

将视图添加/删除到表视图的超级视图后,UITableView 停止滚动

ios7将刷新控件添加到表视图作为子视图后,它增加了额外的空间

当我将核心数据添加到表视图时,我的应用程序崩溃了

SwiftUI 上的 NavigationLink 两次推送视图

必须使用延迟实例化的UI视图将子视图添加到视图控制器两次

以编程方式将视图添加到表视图中的单元格