i am having a tableview with a list of categories.i have an add button which pops an alertview with a textfield.and the text entered in that field should appear in the tableview.everything is fine till here.but it is not getting saved.guess i need to add it to the plist programmatically.can someone help me with it?
//Reading data from plist.
NSString *categoryFile = [[NSBundle mainBundle]pathForResource:@"Categories" ofType:@"plist"];
self.categoryList = [[NSMutableArray alloc]initWithContentsOfFile:categoryFile];
//adding to the tableView
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
UITextField *newCategoryName = [alertView textFieldAtIndex:0];
NSInteger section = 0;
NSInteger row = 0;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
NSString *extraContent = newCategoryName.text;
[[self categoryList]insertObject:extraContent atIndex:row];
NSArray *indexPathsToInsert = [NSArray arrayWithObject:indexPath];
[[self tableOfCategories]insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationRight];
NSLog(@"%@",newCategoryName.text);
}
}
Since the file is located inside your app bundle, then you are not going to be able to save to it
So you will need to save the new array in the Documents Directory
Add the following to your code
This will save the new category to the original file