动态添加并保存数组的条目

时间:2011-11-28 06:25:32

标签: iphone objective-c ios xcode

我有一个带有项目列表的UITableView。如果我添加一个新项目,它会被添加到列表中,但如果我移动到其他视图并返回它只是消失。我希望它保存为那样如果我们删除它,它应该永久删除。我从plist那里得到了物品清单

NSString *fileForCategoryList = [[NSBundle mainBundle] pathForResource:kCATEGORY_LIST ofType:kP_List];
self.arrayForCategories = [[NSMutableArray alloc]initWithContentsOfFile:fileForCategoryList];

这就是我编辑列表的方式。

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView  editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{
// No editing style if not editing or the index path is nil.
if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;
// Determine the editing style based on whether the cell is a placeholder for adding content or already 
// existing content. Existing content can be deleted.    
if (self.editing && indexPath.row == ([self.arrayForCategories count])) 
{
    return UITableViewCellEditingStyleInsert;
} 
else 
{
    return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}

// Update the data model according to edit actions delete or insert.
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

if (editingStyle == UITableViewCellEditingStyleDelete)
{
    [self.arrayForCategories removeObjectAtIndex:indexPath.row];
    [self.tableViewC reloadData];
} 
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:kYOUR_TITLE message:kEMPTY delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:kOK, nil];
    self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
    [self.myTextField setBackgroundColor:[UIColor whiteColor]];
    [myAlertView addSubview:self.myTextField];
    [myAlertView show];
    [myAlertView release];

}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{
if ([self.myTextField text])
    [self.arrayForCategories addObject:[self.myTextField text]];
    [self.tableViewC reloadData];
}   

1 个答案:

答案 0 :(得分:-1)

在app delegate中获取一个数组。当你想添加任何项目时,将它添加到app delegate的数组中。使用相同的数组进行删除。