UITableViewでセルを最後尾に追加したあとスクロールする。

参考サイト
EZ-NET: UITableView を任意の場所へスクロールする : Objective-C プログラミング
UITableView で任意の位置にスクロールさせる – Qiita

- (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];
    
    
    dispatch_async(dispatch_get_main_queue(), ^{ //非同期処理で
        //追加したセルまでスクロールする。
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    });
    [self save];
}

コメントを残す

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