ios - How can I delete an array from a plist using commitEditingStyle? -
i have uitableview populated array keys plist via code:
-(void)viewwillappear:(bool)animated { // paths root direcory nsarray *paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); // documents path nsstring *documentspath = [paths objectatindex:0]; // path our data/plist file nsstring *plistpath = [documentspath stringbyappendingpathcomponent:@"data.plist"]; nsmutabledictionary *dictionary = [nsmutabledictionary dictionarywithcontentsoffile:(nsstring *)plistpath]; viewerkeys = [dictionary allkeys]; [self.tableview reloaddata]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"viewername"]; uilabel *label = (uilabel *)[cell viewwithtag:1000]; label.text = [viewerkeys objectatindex:indexpath.row]; homecoming cell; } i'm trying enable swipe delete on each row in table view, , of course of study means have delete row array in plist. i've tried far:
- (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { nsarray *paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); // documents path nsstring *documentspath = [paths objectatindex:0]; // path our data/plist file nsstring *plistpath = [documentspath stringbyappendingpathcomponent:@"data.plist"]; nsmutabledictionary *dictionary = [nsmutabledictionary dictionarywithcontentsoffile:(nsstring *)plistpath]; nsstring *key = [viewerkeys objectatindex:indexpath.row]; [dictionary removeobjectforkey:[nsstring stringwithformat:@"%@", key]]; nsarray *indexpaths = [nsarray arraywithobject:indexpath]; [tableview deleterowsatindexpaths:indexpaths withrowanimation:uitableviewrowanimationautomatic]; } however isn't writing info plist , error:
invalid update: invalid number of rows in section 0. number of rows contained in existing section after update (2) must equal number of rows contained in section before update (2), plus or minus number of rows inserted or deleted section (0 inserted, 1 deleted) , plus or minus number of rows moved or out of section (0 moved in, 0 moved out).
this plist looks like:
<plist version="1.0"> <dict> <key>(null)</key> <array> <string>username</string> <string>password</string> <string>http://www.google.com</string> <string>/whatever</string> </array> <key>hello</key> <array> <string>admin</string> <string></string> <string>https://www.whatever.com</string> <string>/things</string> </array> </dict> </plist> any help great!
your problem removing object dictionary only. viewerkeys still hold removed objects key. need update viewerkeys before calling deleterowsatindexpaths:
- (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { nsarray *paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); // documents path nsstring *documentspath = [paths objectatindex:0]; // path our data/plist file nsstring *plistpath = [documentspath stringbyappendingpathcomponent:@"data.plist"]; nsmutabledictionary *dictionary = [nsmutabledictionary dictionarywithcontentsoffile:(nsstring *)plistpath]; nsstring *key = [viewerkeys objectatindex:indexpath.row]; [dictionary removeobjectforkey:[nsstring stringwithformat:@"%@", key]]; [dictionary writetofile:plistpath atomically:yes]; viewerkeys = [dictionary allkeys]; nsarray *indexpaths = [nsarray arraywithobject:indexpath]; [tableview deleterowsatindexpaths:indexpaths withrowanimation:uitableviewrowanimationautomatic]; }
ios objective-c uitableview plist
No comments:
Post a Comment