Подтвердить что ты не робот

Как сосредоточиться на последней ячейке в UITableview с прокруткой анимации?

У меня есть UITableview в моем проекте xcode. В этом комментарии перечислены. Как я могу сосредоточиться на последней ячейке моего TableView с прокруткой анимации?

4b9b3361

Ответ 1

Ниже метод найдет последний индекс вашего представления таблицы и сосредоточится на этой ячейке с анимацией

-(void)goToBottom
{
    NSIndexPath *lastIndexPath = [self lastIndexPath];

    [<YOUR TABLE VIEW> scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

Код, чтобы найти последний индекс вашего табличного представления.

-(NSIndexPath *)lastIndexPath
{
    NSInteger lastSectionIndex = MAX(0, [<YOUR TABLE VIEW> numberOfSections] - 1);
    NSInteger lastRowIndex = MAX(0, [<YOUR TABLE VIEW> numberOfRowsInSection:lastSectionIndex] - 1);
    return [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
}

Добавить этот код где-нибудь в вашем контроллере просмотра

[self performSelector:@selector(goToBottom) withObject:nil afterDelay:1.0];

Ответ 2

Имейте в виду, что UITableView наследует UIScrollView

-(void)scrollToBottom:(id)sender
{
    CGSize r = self.tableView.contentSize;
    [self.tableView scrollRectToVisible:CGRectMake(0, r.height-10, r.width, 10) animated:YES];
}

выполнить его как

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(…);
[button addTarget:self action:@selector(scrollToBottom:) forControlEvents:UIControlEventTouchUpInside];

(Вам не нужно использовать performSelector:…)


Другим решением будет выбор последней строки. Вид таблицы позаботится об остальном.

-(void)scrollToBottom:(id)sender
{
    NSInteger lasSection = [self.tableView numberOfSections] - 1;
    NSInteger lastRow = [self.tableView numberOfRowsInSection:lasSection]-1;

    //this while loops searches for the last section that has more than 0 rows.
    // maybe you dont need this check
    while (lastRow < 0 && lasSection > 0) {
        --lasSection;
        lastRow = [self.tableView numberOfRowsInSection:lasSection]-1;
    }
    //if there is no section with any row. if your data source is sane,
    // this is not needed.
    if (lasSection < 0 && lastRow < 0)
        return;

    NSIndexPath *lastRowIndexPath =[NSIndexPath indexPathForRow:lastRow inSection:lasSection];
    [self.tableView selectRowAtIndexPath:lastRowIndexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
}

кнопка одинакова.

Ответ 3

Swift 3:

let indexPath = IndexPath(row: /*ROW TO SCROLL TO*/, section: /*SECTION TO SCROLL TO*/)
tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)