UITableViewで追加する場所

UITableViewで新規にセルを追加した時通常はテーブルの一番上に追加される。
アプリによっては下に追加したい場合もある。

ってなわけで通常であれば下記のような処理だが

- (void)insertNewToDo:(id)sender
{
    if (!objects) {
        objects = [[NSMutableArray alloc] init];
    }
    
    //objects配列に新規辞書配列todo(sender)をいれる。
    [objects insertObject:sender atIndex:0];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    [self Save];
}

indexPathForRowの値をobjects.count-1にすることで下に追加されるようになる。

#pragma mark - segueで戻ってきた時の処置

- (void)insertNewToDo:(id)sender
{
    if (!objects) {
        objects = [[NSMutableArray alloc] init];
    }
    
    //objects配列に新規辞書配列todo(sender)をいれる。
    [objects addObject:sender ];
                                                             //↓ここ
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:objects.count-1 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    [self Save];
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です