5

में UITableView के लिए सामग्री आकार समस्या वास्तव में इंटरफ़ेस बिल्डर के साथ UITableView के साथ UIScrollView बनाते हैं। मैं अपने साथ UIScrollView की सामग्री के आकार सेट:UIScrollView

scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height); 

और मैं UIScrollView

मैं एक IBOutletUITableView बनाया करने के लिए मेरी फाइल के मालिक दृश्य सेट और मैं इंटरफ़ेस बिल्डर में यह निर्धारित किया है।

जब दृश्य लोड होता है। यह मेरे UIScrollView के लिए सही सामग्री आकार प्रदर्शित करता है। लेकिन यह मेरे UITableView के लिए सही सामग्री आकार प्रदर्शित नहीं करता है। इसके अलावा जब मैं अपने UIScrollView के सामग्री आकार को बदलता हूं, तो यह मेरे UITableView के सामग्री आकार को बदलता है जैसे कि दोनों सामग्री का आकार संबंधित था।

कोई भी जानता है कि मैं अपने टेबल व्यू के सामग्री आकार को कैसे रख सकता हूं जिसे मैंने इंटरफ़ेस बिल्डर में सेट किया है?

उत्तर

12

UITableViewUIScrollView है। बहुत कम समय होते हैं जब UIScrollView के अंदर UITableView एम्बेड करना उपयोगी होगा।

मान लीजिए कि आप वास्तव में यह चाहते हैं, आपको निम्नलिखित जगहों को करने की आवश्यकता होगी। शायद viewDidAppear: या आपके द्वारा तालिका दृश्य को पॉप्युलेट करने के बाद कहीं भी UIViewController में या कहीं भी।

// Set the size of your table to be the size of it's contents 
// (since the outer scroll view will handle the scrolling). 
CGRect tableFrame = tableView.frame; 
tableFrame.size.height = tableView.contentSize.height; 
tableFrame.size.width = tableView.contentSize.width; // if you would allow horiz scrolling 
tableView.frame = tableFrame; 

// Set the content size of your scroll view to be the content size of your 
// table view + whatever else you have in the scroll view. 
// For the purposes of this example, I'm assuming the table view is in there alone. 
scrollView.contentSize = tableView.contentSize; 
+0

यह अभी भी काम नहीं करता है ... वास्तव में जब मैं NSLog tableView.frame मुझे प्रदर्शित करता है (शून्य)। मुझे समझ में नहीं आता ... – kschaeffler

+0

दरअसल, मेरी फ़ाइल का मालिक UIScrollView और UITableView का प्रतिनिधि है जो एक स्क्रॉल व्यू है। कोई समस्या है? – kschaeffler

+0

मुझे और अधिक स्पष्ट होना चाहिए था। 'tableView' और 'scrollView' को उन आइटम्स से जुड़े आईबीओटलेट माना जाता है। – DBD

0

यदि आपको अभी भी समस्याएं आ रही हैं, तो स्क्रॉल व्यू की आवश्यकता के बिना इसे आजमाएं।

थोड़ी देर के लिए DBD के उदाहरण के साथ चारों ओर खेलने के बाद, मैंने पाया कि फ्रेम और contentSize एक साथ सेट किया जा करने में सक्षम होना प्रतीत नहीं चाहते:

self.tableView.contentSize = CGSizeMake(320, scrollHeight); 
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,self.tableView.contentSize.height); 

प्रयास की एक अधिकतम ऊंचाई निर्धारित करने के लिए था फ्रेम लेकिन बड़ी संख्या में कोशिकाओं की संख्या में हालांकि सभी सामग्री को स्क्रॉल करने की क्षमता रखें। यह हैक अच्छी तरह से काम करता प्रतीत होता है और यदि आवश्यक हो तो अधिक स्थितियों के साथ संशोधित किया जा सकता है:

int cellCount = [YOURARRAY count]; 
CGFloat scrollHeight = cellCount*44+44;//for a cell with height of 44 and adding 44 if you have a toolbar at the bottom 

self.tableView.contentSize = CGSizeMake(320, scrollHeight);//you can change the width and or let the .xib control the autoresizing 

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && cellCount < 14) 
{ 
    //here's were you can add more conditions if you're in landscape or portrait, but this handles 14 cells with a heightForHeaderInSection of 46 in landscape 
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,scrollHeight); 
} 
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && cellCount < 7) 
{ 
    //same as above but it's for the iPhone in portrait 
    self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,44,self.tableView.contentSize.width,scrollHeight); 
} 

यह सुनिश्चित करने के लिए काम करता है। आपको अपने .xib या अपने कोड में ऑटोरेसाइजिंगमास्क को समायोजित करने की आवश्यकता हो सकती है, लेकिन यह हैक एकमात्र समाधान है जो मैंने पाया है जो मेरे मामले में सभी अलग-अलग चर का ख्याल रखता है।

1

उदाहरण गतिशील scrollview के साथ प्रबंधन करने के लिए tableview ऊंचाई, स्विफ्ट में काम करता है:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     // Custom Cell 
     let cell:TblPortCell = self.tableList.dequeueReusableCellWithIdentifier("cell") as! TblPortCell 

     //tableList is the OutLet for the table 
     tableList.sizeToFit() 
     // Set the size of the table to be the size of it's contents 
     // (since the outer scroll view will handle the scrolling). 
     let height = tableList.frame.size.height 
     let pos = tableList.frame.origin.y 
     let sizeOfContent = height + pos + 130 
     //scrollView is the Outlet for the Scroll view 
     scrollView.contentSize.height = sizeOfContent 
     scrollView.sizeToFit() 

     cell.selectionStyle = UITableViewCellSelectionStyle.None 
     return cell 
    } 

संदर्भ के लिए:

0

विधि कई नीचे कॉल बार। प्रत्येक बार जब आप कॉल करेंगे तो अधिक कोशिकाएं होंगी।

CGRect tableFrame = tableView.frame; 
tableFrame.size.height = tableView.contentSize.height; 
tableView.frame = tableFrame; 

scrollView.contentSize = tableView.contentSize;