使用uitableview从plist文件中删除数据

时间:2010-09-21 17:23:52

标签: iphone objective-c

我有一个plist文件被带入tableview,现在想要删除我过去添加的项目,但是甚至不知道从哪里开始。我有编辑工作,它在每一行显示,任何帮助超过欢迎!

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return amounts.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //Create Cell
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

    //Fill cell
    NSDictionary *budgetItem = [amounts objectAtIndex:indexPath.row];
    currentNumber = [[budgetItem objectForKey:@"Value"] floatValue];

    NSString *imageFile = [[NSBundle mainBundle] pathForResource:@"mini" ofType:@"png"];
    UIImage *ui = [[UIImage alloc] initWithContentsOfFile:imageFile];
    cell.imageView.image = ui;
    result = result + currentNumber;


    cell.textLabel.text = [NSString stringWithFormat:@"£%.2f",currentNumber];
    NSString *detailTxt = [NSString stringWithFormat:@"%@ - %@", [budgetItem objectForKey:@"Description"], [budgetItem objectForKey:@"Date"]];
    cell.detailTextLabel.text = detailTxt;

    //Return it
    return cell;
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}


- (void) loadData{

    //Get file location
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"saveBudget.plist"];

    // Load items
    NSString *error;
    NSPropertyListFormat format;
    NSData *plistData = [NSData dataWithContentsOfFile:path];
    NSArray *amountData = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];

    // Add Edit Button
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

- (void) setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing: editing animated: animated];
    [dataTable setEditing:editing animated:animated];
}

1 个答案:

答案 0 :(得分:1)

您将文件中的数据读入可变容器(最有可能的数组)。然后使用UITableView提供的方法对容器进行更改以响应用户操作。接下来,将容器再次写回磁盘。