2015-12-23 10 views
5

मैं तालिका दृश्य में सभी सामग्री के लिए ऊंचाई से मेल खाने के लिए UITableView सेट करना चाहता हूं।गतिशील UITableView ऊंचाई

यह मेरा स्टोरीबोर्ड

enter image description here

इस के साथ समस्या यह शीर्ष है और नीचे imageView हमेशा स्क्रीन पर स्थिर है।

enter image description here

वहाँ तालिका दृश्य पर 10 आइटम होने के लिए लगता है कि कर रहे हैं, लेकिन केवल 7 स्क्रीन आकार सीमा के कारण दिखाई देता है। उपयोगकर्ता नीचे छवि ImageView देखने में सक्षम होने से पहले मैं सभी 10 दिखाना चाहता हूं। (Btw, विचारों के सभी 3 अर्थात्। दोनों छवि विचारों और tableview एक UIScrollView में है)

आदर्श

enter image description here

अन्य सीमाओं मैं के साथ काम करने के लिए है कि में से कुछ है कि है तालिका दृश्य में वस्तुओं की संख्या गतिशील है जिसका अर्थ है कि यह आमतौर पर 10 से कम की किसी भी मात्रा में हो सकता है जिसे बाद में मैं एपीआई से पुनर्प्राप्त करूंगा। और सामग्री की ऊंचाई के आधार पर सेल ऊंचाई भी गतिशील है।

मैं केवल बस कुछ सरल कोड

class ExampleViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
     @IBOutlet weak var tableView: UITableView! 

    var items: [String] = [ 
    "Item 01", "Item 02", "Item 03", "Item 04", "Item 05", 
    "Item 06", "Item 07", "Item 08", "Item 09", "Item 10"] 

    override func viewDidLoad() { 
    super.viewDidLoad() 
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return self.items.count; 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell 
    cell.textLabel?.text = self.items[indexPath.row] 
    return cell 
    } 

} 
+0

आप self.items.count * row_height इस सामग्री के आधार पर ऊंचाई दे देंगे की कोशिश की थी, लेकिन यू भी स्क्रीन ऊंचाई की जांच करने के – Smile

+0

क्यों आप शीर्षक में ऊपर और नीचे imageViews न जोड़ें अन्य शर्तों लिखने के लिए की जरूरत है और टेबलव्यू के पाद लेख? – Muneeba

+0

शीर्षलेख और पाद लेख वास्तव में काम नहीं करते हैं क्योंकि ऊपर और नीचे छवि दृश्य केवल विचारों का प्रतिनिधि है। वास्तव में, यह अधिक घटकों, लेबल, छवियों और बाधाओं के साथ एक दृश्य है। – WKL

उत्तर

2

आप NSLayoutConstraint कि tableView ऊंचाई (पहले आपके द्वारा बनाए गए आवश्यकता सेट करने के लिए एक IBOutlet निर्धारित करने की आवश्यकता किसी भी मूल्य के साथ ऊंचाई बाधा, कोई फर्क नहीं पड़ता) और फिर ctrl इसे अपनी कक्षा फ़ाइल

पर खींचें

enter image description here

फिर आपके viewWillAppear में आपको tableView ऊंचाई की गणना करना होगा और इसे सेट करना होगा।इस तरह:

var tableViewHeight:CGFloat = 0; 
for (var i = tableView(self.tableView , numberOfRowsInSection: 0) - 1; i>0; i-=1){ 
    tableViewHeight = height + tableView(self.tableView, heightForRowAtIndexPath: NSIndexPath(forRow: i, inSection: 0)) 
} 
tableViewHeightLayout.constant = tableViewHeight 

और यह बहुत अधिक है। इससे आपकी स्क्रॉल सामग्री सामग्री दिखाई देगी और किसी भी चेतावनी को नहीं बढ़ाया जाना चाहिए।

1

उस मामले में साथ शुरू कर दिया है, आपके नीचे सेल स्थिर नहीं करते हैं, यह तालिका दृश्य का एक हिस्सा बनाने के लिए और तालिका का उपयोग कर अंतिम पंक्ति में इस तल छवि डालने प्रतिनिधि विधि देखें - insertRowAtIndexPath

1

इस प्रकार के मामले में एक टेबल फ़ूटर व्यू में अपनी निचली छवि दृश्य (लाल) जोड़ें।

पाद लेख दृश्य जोड़ने के लिए UITableView में आप का उपयोग कर सकते हैं:

tableViewObj.tableFooterView = footerViewObj; 
1

आप शायद तालिका दृश्य आंतरिक सामग्री आकार को लागू करने की है। यह देखने के लिए कि यह मदद करता है, कृपया answer देखें।

मुझे यह समस्या याद है और यहां तक ​​कि एक कस्टम UITableView सबक्लास बनाया गया है।

#import "IntrinsicTableView.h" 

@implementation IntrinsicTableView 

#pragma mark - UIView 

- (CGSize)intrinsicContentSize 
{ 
    return CGSizeMake(UIViewNoIntrinsicMetric, self.contentSize.height); 
} 

#pragma mark - UITableView 

- (void)endUpdates 
{ 
    [super endUpdates]; 
    [self invalidateIntrinsicContentSize]; 
} 

- (void)reloadData 
{ 
    [super reloadData]; 
    [self invalidateIntrinsicContentSize]; 
} 

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 
{ 
    [super reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation]; 
    [self invalidateIntrinsicContentSize]; 
} 

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation 
{ 
    [super reloadSections:sections withRowAnimation:animation]; 
    [self invalidateIntrinsicContentSize]; 
} 

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 
{ 
    [super insertRowsAtIndexPaths:indexPaths withRowAnimation:animation]; 
    [self invalidateIntrinsicContentSize]; 
} 

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation 
{ 
    [super insertSections:sections withRowAnimation:animation]; 
    [self invalidateIntrinsicContentSize]; 
} 

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 
{ 
    [super deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation]; 
    [self invalidateIntrinsicContentSize]; 
} 

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation 
{ 
    [super deleteSections:sections withRowAnimation:animation]; 
    [self invalidateIntrinsicContentSize]; 
} 

@end 
1

सूचकांक पथ पर प्रयास करें पंक्ति के लिए यह भी

in ViewDidLoad 

self.table.estimatedRowHeight = 44.0 ; 
self.table.rowHeight = UITableViewAutomaticDimension; 

ऊंचाई

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
return UITableViewAutomaticDimension;} 
4
  1. उपवर्ग अपने UITableView इसके contentSize होने की intrinsicContentSize ओवरराइड करने के लिए, इस तरह:

    override var intrinsicContentSize: CGSize { 
        return contentSize 
    } 
    
  2. फिर अपनी मेज के लिए स्वत: पंक्ति ऊंचाइयों उपयोग करते हैं, तो अपने exampleViewController के viewDidLoad होगा:

    tableView.estimatedRowHeight = 44 
    

    और UITableViewDelegate फ़ंक्शन:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
        return UITableViewAutomaticDimension 
    } 
    
  3. आप अपने एपीआई से डेटा प्राप्त करें और अपनी मेज को फिर से लोड करते हैं, सिर्फ फोन:

    tableView.invalidateIntrinsicContentSize() 
    

    यह (ओवरराइड की वजह से) इसकी सामग्री के समान आकार में ही आकार बदलने के लिए अपनी मेज बता देंगे, और अपनी निचली छवि को आवश्यकतानुसार ले जाएं।


अपने स्टोरीबोर्ड कह UITableView पर कोई ऊंचाई बाधा है क्योंकि अपने UIScrollView एक अस्पष्ट ऊंचाई है कि एक त्रुटि फेंकता है, तो अपने UITableView चुनें और इसे आकार निरीक्षक में एक प्लेसहोल्डर आंतरिक आकार देते हैं।

+1

कमाल! मैं वास्तव में विश्वास नहीं कर सकता कि यह आईओएस का हिस्सा नहीं है। क्या यह वास्तव में आम उपयोग नहीं है? –

+0

धन्यवाद @ मार्टिन मैसेरा! मैं मानता हूं, यह समझने में मुझे कुछ समय लगा कि यह कैसे करना है, लेकिन मैंने इस दृष्टिकोण का कई बार उपयोग किया है। मैं subclass नहीं करना चाहता था, लेकिन मैंने सोचा कि यह अभी भी बाधाओं को मैन्युअल रूप से अद्यतन करने के बजाय सबसे साफ था। – Cody

+0

कमाल! सबसे अच्छा उपाय – Mikhail

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