swift - My cells are duplicating themselves -
i new swift , trying make note app. have split view controller goes in first view controller , view controller connects table view controller. works when launch app have notes want when try go first view controller , come table view controller, notes duplicated every single time it. tried can try, there can me masterviewcontroller is
import uikit class masterviewcontroller: uitableviewcontroller { var detailviewcontroller: detailviewcontroller? = nil override func viewdidload() { super.viewdidload() note.loadnotes() // problem here, think notetable = self.tableview // additional setup after loading view, typically nib. let addbutton = uibarbuttonitem(barbuttonsystemitem: .add, target: self, action: #selector(insertnewobject(_:))) navigationitem.rightbarbuttonitem = addbutton if let split = splitviewcontroller { let controllers = split.viewcontrollers detailviewcontroller = (controllers[controllers.count-1] as! uinavigationcontroller).topviewcontroller as? detailviewcontroller } }
my loadnotes function is
class func loadnotes() { let defaults:userdefaults = userdefaults.standard let savedata: [nsdictionary]? = defaults.object(forkey: kallnotes) as? [nsdictionary] if let data:[nsdictionary] = savedata { i:int in 0 ..< data.count { let n:note = note() n.setvaluesforkeys(data[i] as! [string : any]) allnotes.append(n) } } }
your loadnotes
method keeps appending. first line of loadnotes
should be:
allnotes = [note]()
then starts empty array , fills up.
and why loadnotes
static method? that's bad design. make notes
normal class , make loadnotes
instance method.
on unrelated note (no pun intended), not use userdefaults
store app data. use store little bits of information.
Comments
Post a Comment