2013-02-07 8 views

Odpowiedz

26

Poniżej metoda znajdzie ostatni indeks wyświetlanie tabeli i będzie koncentrować się na tej komórki z animacją

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

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

kod do znalezienia ostatniego indeksu swojego tableview.

-(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]; 
} 

Dodaj ten kod gdzieś w kontrolerze widoku

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

dzięki koleś ..... ........ działa wielkie dzięki :) –

+0

działa dobrze ... :) – jaydev

+0

prace jak mistrz – iLearner

2

Pamiętaj, że UITableView dziedziczy od UIScrollView

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

wykonać go jak

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

(Nie trzeba używać performSelector:…)


Innym rozwiązaniem byłoby wybranie ostatniego wiersza. Widok tabeli zajmie się resztą.

-(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]; 
} 

przycisk jest taki sam.

+0

zobacz moją edycję przewijania, wybierając. – vikingosegundo

+0

Naprawiono drobne błędy. zwróć uwagę, że jeśli jesteś pewien, że 'numberOfRowsInSection' zawsze zwróci wartość o 1 lub wyższą, możesz usunąć instrukcję while i następującą instrukcję if. – vikingosegundo

+0

To rozwiązanie wymaga zdarzenia kliknięcia przycisku, aby przejść do ostatniej komórki. Możesz użyć performSelector z opóżnieniem, aby przejść do ostatniej komórki po załadowaniu całego widoku tabeli. –

0

Swift 3:

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