2010-07-13 19 views

उत्तर

21

अपने UITableViewDelegate में tableView:willDisplayCell:forRowAtIndexPath: विधि लागू करें और यह जांचें कि यह अंतिम पंक्ति है या नहीं।

+1

टिप के लिए धन्यवाद। मैं वास्तव में एक घटना को ट्रिगर करने के लिए इस विधि का उपयोग कर रहा हूं और ऐसा लगता है क्योंकि यह "विल" प्रदर्शित होता है, यह वास्तव में सेल को प्रदर्शित करने से पहले होता है। क्या सेल के प्रदर्शित होने के बाद आग लगती है? – Ward

+0

अलास, नहीं। आप 'प्रदर्शन चयनकर्ता: withObject: afterDelay' का उपयोग कर थोड़ा सा विवाद कर सकते हैं, जिसकी आपको आवश्यकता होती है उसे कॉल करने के लिए बहुत ही कम देरी होती है। –

+0

@ArtGillespie ठीक वही मिला जो मैं खोज रहा था। चीयर्स! –

37
tableView:cellForRowAtIndexPath: अंदर

या tableView:willDisplayCell:forRowAtIndexPath: इस तरह:

- (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

आपने शायद कहा होगा कि मैं एक डमी हूं। मदद के लिए धन्यवाद कभी-कभी मुझे लगता है कि ये चीजें उनके मुकाबले कठिन हैं। – Ward

+3

संख्याऑफसेक्शन कॉलसोर्स नंबर कॉल करता हैऑफसेक्शन इनटेबल व्यू और नंबरऑफ्रोइन्सइनसेक्शन कॉलसोर्स नंबरऑफ्रोइन्स इनसेक्शन कॉल करता है। और मेरे कार्यान्वयन में यह इस निष्पादन के इस रिकर्सिव लॉग को दिखाकर दुर्घटनाग्रस्त हो गया। –

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

Xcode 8 के लिए और तेजी से 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

Xcode 8 और 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

के लिए सुरुचिपूर्ण एक्सटेंशन बनाएं 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 
    } 
} 

उपयोग के उदाहरण:

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

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

    return cell 
} 
0

मैं willDisplayCell विधि के साथ कुछ समस्या थी। यह मेरे लिए काम करता है:

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") 
    } 
} 

बस प्रतिनिधि तरीकों में यह पारित, और परिवर्तन 'तालिका' अपने tableView करने के लिए।

संबंधित मुद्दे