2012-09-20 15 views
17

आज सुबह मैं सिर्फ नए Xcode स्थापित जो आईओएस 6आईओएस UITableView scrollToRowAtIndexPath अब

मैं एक तालिका दृश्य एक plist अध्याय और लाइनों युक्त फ़ाइल के साथ भरी हुई है शामिल काम नहीं कर रहा। अध्याय अनुभागों को परिभाषित करते हैं।

उपयोगकर्ता अध्याय और रेखा का चयन करता है और तालिकादृश्य स्वचालित स्थिति (दृश्यडिडलोड में) में स्वचालित रूप से स्क्रॉल किया जाता है।

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:linePos inSection:chapterPos]; 
[self.tableView scrollToRowAtIndexPath:indexPath 
    atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

यह आईओएस 5 सिम्युलेटर में बस महान काम करता है।

आईओएस 6 सिम्युलेटर में इसे करने का प्रयास स्क्रॉल नहीं किया जाता है। मुझे कोई त्रुटि नहीं है। मैंने जांच की है, linePos और ChapterPos सही मान प्राप्त करते हैं लेकिन स्क्रॉल नहीं किया जाता है।

कोई विचार क्यों?

+4

सिर्फ दर्द और दुख अपने 5 साल बाद आईओएस 11. –

उत्तर

26

आईओएस के हाल के संस्करणों के लिए, कृपया फ्योडोर वोल्च्योक के जवाब को पढ़ें। ध्यान दें कि इसे स्वीकार किए गए उत्तर के रूप में चिह्नित नहीं किया गया है क्योंकि उस समय जब प्रश्न पूछा गया था (सितंबर 2012), वर्तमान उत्तर कार्य समाधान था। आईओएस के हाल के संस्करणों को भी वही समस्या मिली जो अब फ्योडोर वोल्च्योक के जवाब से हल हो गई है, इसलिए आपको उस वक्त अपना जवाब +1 करना चाहिए।


मुझे जवाब मिला। मैं पहली बार tableview

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:linePos inSection:chapterPos]; 

[self.tableView reloadData]; 

[self.tableView scrollToRowAtIndexPath:indexPath 
    atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

हालांकि मैं इस सवाल का जवाब मैं क्यों यह iOS5 में iOS6 में नहीं काम कर रहा है और पता नहीं है पाया में डेटा को फिर से लोड करने के लिए है।

संपादित

शायद मैं जोड़ने चाहिए कि भले ही यह काम कर रहा था, मैं अभी भी अंतिम पंक्ति प्रदर्शित करने में कोई समस्या हो रही थी और उस

UItableview scrollToRowAtIndexPath not displaying last row correctly

के रूप में @ के लिए एक प्रश्न पोस्ट राज ने इसके लिए भी पूछा, मुझे कहना चाहिए कि मैं viewDidLoad में ट्रिगर कर रहा था। आखिरी पंक्ति की समस्या को सही ढंग से प्रदर्शित नहीं करने के लिए मुझे इसे viewDidAppear में रखना पड़ा।

+0

इस जानकारी के लिए धन्यवाद में फिर से टूट में जोड़ने के लिए, मैं सिर्फ एक ही बात में फंस गयी। मैंने यह भी देखा कि यह तालिका स्क्रॉलिंग को एनिमेट नहीं करता है, बस इसे चयनित पंक्ति पर प्रदर्शित करता है। –

+0

क्या आप अपनी -viewDidLoad विधि में scrollToRowAtIndexPath कॉल को ट्रिगर कर रहे हैं? –

40

ऑब्जेक्टिव-सी

[self.tableView reloadData]; 
dispatch_async(dispatch_get_main_queue(), ^{ 
     NSIndexPath *rowIndexPath = [NSIndexPath indexPathForRow:3 inSection:0]; 
     [self.tableView scrollToRowAtIndexPath:rowIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 
}); 

स्विफ्ट

tableView.reloadData() 
DispatchQueue.main.async { 
    let indexPath = IndexPath(row: linePos, section: chapterPos) 
    self.tableView.scrollToRow(at: indexPath, at: .top, animated: true) 
} 
+0

यह एक महत्वपूर्ण फिक्स है, इसके बिना ऐप रीलोड लोड प्रक्रियाओं के आधार पर गलती से दुर्घटनाग्रस्त हो सकता है। – BootMaker

+3

यह एकमात्र चीज है जो मेरे लिए काम करती है (आईओएस 9.1)। 'ScrollToRowAtIndexPath' हमेशा के लिए बहुत नीचे सेल स्क्रॉल करने में विफल रहा, जब तक कि मैं' dispatch_async में लपेटा (dispatch_get_main_queue(), {...} '' भी हालांकि scrollToRowAtIndexPath' पहले से ही मुख्य थ्रेड से बुलाया जा रहा था। –

+0

यह शायद सही जवाब है , प्लस इसे दृश्य में डाला गया DidAppear: –

1

iOS7 संपत्ति UIViewController डिफ़ॉल्ट की automaticallyAdjustsScrollViewInsetsYES हो जाने के बाद। दृश्य नियंत्रक धक्का देने पर सिस्टम तालिका दृश्य के contentOffset को समायोजित करने का कारण बन जाएगा। यहां तक ​​कि viewWillAppear में आप [self.tableView scrollToRowAtIndexPath:rowIndexPath atScrollPosition:UITableViewScrollPositionNone animated:NO]; पर भी कॉल करते हैं। viewWillAppear के बाद contentOffset सिस्टम द्वारा भी बदला जाएगा। तो मेरा समाधान है:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.automaticallyAdjustsScrollViewInsets = NO; 

    /// any other codes 
} 

- (void)viewWillLayoutSubviews { 
    self.tableView.contentInset = UIEdgeInsetsMake(self.topLayoutGuide.length, 0, 0, 0); 
} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    // change tableView data source 

    [self.tableView reloadData]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.dataSourceArray count] - 1 inSection:0]; 
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:NO]; 
} 
0

मैं इस जवाब को फ्योडोर वोल्च्योक के उत्तर के अतिरिक्त जोड़ रहा हूं। मैंने यह भी पाया कि प्रेषण इस मुद्दे को हल करता है। मैं एक वर्कअराउंड खोजने में सक्षम था जो प्रेषण नहीं करता है।

self.tableView.reloadData() 
let index = // the desired index path 

// For some reason, requesting the cell prior to 
// scrolling was enough to workaround the issue. 
self.tableView.cellForRowAtIndexPath(index) 

self.tableView.scrollToRowAtIndexPath(index, atScrollPosition: .Top, animated: false) 
+0

मुझे multithreading के साथ कोई समस्या थी: जब मैं प्रेषण का उपयोग करता हूं, तो मेरी विधियों में से एक दो बार कॉल करना शुरू कर रहा है। और अब यह जादू की तरह काम करता है !!! :-) –

0
स्विफ्ट में

फ्योदोर Volchyok का जवाब:

tableView.reloadData() 
let indexPath = NSIndexPath(forRow: linePos, inSection: chapterPos) 
// make sure the scroll is done after data reload was finished 
dispatch_async(dispatch_get_main_queue()) { 
    self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true) 
} 
0

यह एक iPhone एक्स पर विशेष रूप से ios11

self.tableView.estimatedRowHeight = 0; 
self.tableView.estimatedSectionFooterHeight = 0; 
self.tableView.estimatedSectionHeaderHeight = 0 

dispatch_async(dispatch_get_main_queue(), ^{ 
    NSInteger numberOfSections = self.tableView.numberOfSections; 
    if (numberOfSections > 0) 
    { 
    NSInteger lastSection = numberOfSections - 1; 
    NSInteger lastRowInLastSections = [self.tableView numberOfRowsInSection:lastSection] - 1; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastRowInLastSections inSection:lastSection]; 
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:isAnimated]; 
} 
}); 
1

मैं scrollToRowAtIndexPath की किसी अन्य समस्या (शायद एक बग) में भाग में मेरे लिए काम किया ios11 चल रहा है। मेरी तालिका में कुछ सौ वर्ग हैं और ढहने वाले मोड ~ 10 दृश्यमान स्क्रीन में फिट होंगे। इंडेक्सपैथ गहरा हो गया, स्क्रॉलिंग धीरे-धीरे पीछे गिर गई।

उदाहरण के लिए, जब मैं खोज पंक्ति 30 में आइटम चाहते थे, ScrollPositionTop वास्तविक पंक्ति मैं शीर्ष पर होने की उम्मीद से पहले एक अतिरिक्त पंक्ति के लिए होगा।

और जैसा कि मैं गहरी पंक्तियों के लिए खोज परीक्षण किया है, इसके पीछे और भी अधिक है, जहां के लिए कहते हैं कि पिछले 100 पंक्तियों गहरी या ऐसा है, तो उम्मीद पंक्ति भी दिखाई दे क्षेत्र में नहीं आया था कुछ भी गिरने शुरू कर दिया।

वैकल्पिक हल मैं अब तक पाया एनिमेटेड कहना है: dispatch_async भीतर स्क्रॉल के लिए नहीं है, तो यह किसी भी खामियों के बिना काम करता है।

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