2010-07-13 5 views

Répondre

21

Implémentez la méthode tableView:willDisplayCell:forRowAtIndexPath: dans votre UITableViewDelegate et vérifiez s'il s'agit de la dernière ligne.

+1

merci pour le conseil. J'utilise cette méthode pour déclencher un événement et il semble que l'affichage de "Will" apparaisse avant que la cellule ne s'affiche. Y a-t-il une méthode qui se déclenche après l'affichage de la cellule? – Ward

+0

Hélas, non. Vous pouvez légèrement fudge en utilisant 'execSelector: withObject: afterDelay' avec un très court délai pour appeler une méthode qui fait ce dont vous avez besoin. –

+0

@ArtGillespie J'ai trouvé exactement ce que je cherchais. À votre santé ! –

37

intérieur tableView:cellForRowAtIndexPath: ou tableView:willDisplayCell:forRowAtIndexPath: comme ceci:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    ... 

    NSInteger sectionsAmount = [tableView numberOfSections]; 
    NSInteger rowsAmount = [tableView numberOfRowsInSection:[indexPath section]]; 
    if ([indexPath section] == sectionsAmount - 1 && [indexPath row] == rowsAmount - 1) { 
     // This is the last cell in the table 
    } 

    ... 

} 
+0

vous pourriez aussi bien avoir dit que je suis un mannequin. merci pour l'aide parfois je pense que ces choses sont plus difficiles qu'elles ne le sont. – Ward

+3

numberOfSections appelle la source de données numberOfSectionsInTableView et numberOfRowsInSection appelle la source de données numberOfRowsInSection. Et dans ma mise en œuvre, il s'est écrasé en affichant ce journal récursif de cette exécution. –

14
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSInteger lastSectionIndex = [tableView numberOfSections] - 1; 
    NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1; 
    if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) { 
     // This is the last cell 
    } 
} 
1

Pour Xcode 8 et rapide 3

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
     let lastSectionIndex = tableView.numberOfSections - 1 
     let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1 
     if indexPath.section == lastSectionIndex && indexPath.row == lastRowIndex { 
      print("this is the last cell") 
     } 
    } 
2

Pour Xcode 8 et swift3

//Show Last Cell (for Table View) 
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 
{ 
    if indexPath.row == (yourdataArray.count - 1) 
    { 
     print("came to last row") 
    } 
} 

//Show last cell (for Collection View) 
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) 
{ 
    if indexPath.row == (yourdataArray.count - 1) 
    { 
     // Last cell is visible 
    } 
} 
3

Créer l'extension élégante pour UITableView:

extension UITableView { 

    func isLast(for indexPath: IndexPath) -> Bool { 

     let indexOfLastSection = numberOfSections > 0 ? numberOfSections - 1 : 0 
     let indexOfLastRowInLastSection = numberOfRows(inSection: indexOfLastSection) - 1 

     return indexPath.section == indexOfLastSection && indexPath.row == indexOfLastRowInLastSection 
    } 
} 

Exemple d'utilisation:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if tableView.isLast(for: indexPath) { 
     //do anything then 
    } 

    return cell 
} 
0

J'ai eu quelques problèmes avec la méthode willDisplayCell. Cela fonctionne pour moi:

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    let y = scrollView.contentOffset.y/(scrollView.contentSize.height - scrollView.frame.size.height) 
    let relativeHeight = 1 - (table.rowHeight/(scrollView.contentSize.height - scrollView.frame.size.height)) 
    if y >= relativeHeight{ 
     print("last cell is visible") 
    } 
} 

Il suffit de passer cela dans les méthodes de délégué et de modifier «table» à votre tableView.

Questions connexes