0

ModalViewController を呼び出す TableViewController である単純なプログラムがあります。ユーザーはテキスト フィールドにテキストを追加し、[保存] をクリックすると、TableViewController に追加されます。

スワイプして行を削除しようとしていますが、削除すると、値が「NULL」に変わり、行がそのまま残ります。アプリを再起動するか、別のビュー コントローラーに移動してもう一度戻ると、行が消えます。

私のコードは次のようになります。

@interface NewTimelineViewController () <UITableViewDataSource, UITableViewDelegate>
@property (strong) NSMutableArray *transactions;
@end

@implementation NewTimelineViewController

@synthesize transactions = _transactions;

- (NSManagedObjectContext *)managedObjectContext
{       
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication]delegate];
    if ([delegate performSelector:@selector(managedObjectContext)])
    {
        context = [delegate managedObjectContext];
    }
    return context;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self.managedObjectContext deleteObject:[self.transactions objectAtIndex:indexPath.row]];

        NSError *error = nil;
        if (![self.managedObjectContext save:&error])
        {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            //abort();
            //[self.tableView reloadData];       
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Persons";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    NSManagedObject *transaction = [self.transactions objectAtIndex:indexPath.row];

    [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@", [transaction valueForKeyPath:@"whoBy.name"], [transaction valueForKeyPath:@"gifting.amount"]]];

    return cell;
}

それはかなり簡単なことだと思います。私には、このビューで実際の行が削除または再ロードされていないように見えますが、この TableView が再び表示された瞬間に消えてしまいます。

どんな助けでも大歓迎です!

4

3 に答える 3

0

データソース (おそらく配列) からもオブジェクトを削除してから、テーブル ビューをリロードする必要があります (リロードまたは reloadRowsAtIndexPaths のような目立たない方法のいずれかを使用)。

于 2013-10-12T12:03:25.733 に答える