2016-07-21 8 views
5

हाय सबको। मैंने 1 महीने पहले प्रोग्रामिंग सीखना शुरू किया था, इसलिए अगर मैं अपनी समस्या को अच्छी तरह से समझा नहीं तो कृपया अच्छा रहें :)स्विफ्ट: UICollectionView के आकार के आधार पर UITableViewCell ऊंचाई का विस्तार करें

मेरी परियोजना मुख्य UITableView से बना है। UITableView के प्रत्येक कक्ष में, मेरे पास UICollectionView (क्षैतिज स्क्रॉलिंग पर) है।

Img 1 : Main view

प्रत्येक UICollectionViewCell की चौड़ाई पूरे UITableViewCell के समान है। मेरी पहली समस्या UITableViewCell की ऊंचाई का आकार (जो UICollectionView के आकार और इसके शीर्ष पर सामग्री का आकार) पर निर्भर करेगा।

Img 2 : CollectionView

यह स्वचालित रूप से किया जा सकता है। वास्तव में, UICollectionViewCell एस समान ऊंचाई नहीं है, इसलिए जब उपयोगकर्ता क्षैतिज UICollectionView स्क्रॉल करेगा, तो एक नया UICollectionViewCell दिखाई देगा (विभिन्न ऊंचाई के साथ) और UITableViewCell की ऊंचाई को अनुकूलित करना होगा।

मेरी दूसरी समस्या UICollectionViewCell का आकार बदलने के बारे में है, वास्तव में मैं पहले से नहीं जानूंगा कि इसकी ऊंचाई क्या होगी (चौड़ाई UITableView के समान है)। मुझे एक निब से सामग्री आयात करना चाहिए।

@IBOutlet weak var tableView: UITableView! 
var storedOffsets = [Int: CGFloat]() 

UITableView विस्तार: अंदर यह

extension IndexVC: UITableViewDelegate, UITableViewDataSource { 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 6 //Temp 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if let cell = tableView.dequeueReusableCellWithIdentifier("CellCollectionView") as? CellPost { 
     let post = self.posts[indexPath.row] 
     cell.configureCell(post) 

     return cell 
    } else { 
     return CellPost() 
    } 

} 

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    guard let tableViewCell = cell as? CellPost else { return } 

    tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row) 
    tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0 
} 

func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    guard let tableViewCell = cell as? CellPost else { return } 

    storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset 
} 

UITableView की सेल और UICollectionView के प्रतिनिधि सेट बनाएं

तो अब यहाँ मेरी ViewController फ़ाइल,

चर है UICollectionView का हिस्सा: एक निब/xib से सेल

पर दृश्य जोड़ें
extension IndexVC: UICollectionViewDelegate, UICollectionViewDataSource { 
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 9 //Temp 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellInCollectionView", forIndexPath: indexPath) 

    if let textPostView = NSBundle.mainBundle().loadNibNamed("textPostView", owner: self, options: nil).first as? textPostView { 
     textPostView.configurePost(post.descriptionLbl) 
     cell.addSubview(textPostView) 
     textPostView.translatesAutoresizingMaskIntoConstraints = false 
     cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":textPostView])) 
     cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":textPostView])) 

    } 

    return cell 
} 

पूरे UICollectionView

extension IndexVC: UICollectionViewDelegateFlowLayout { 
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return CGSizeMake(collectionView.frame.width, collectionView.frame.height) 
} 

के रूप में सेल के आकार में एक ही बनाने और मैं वर्ग UITableViewCell (समस्या के लिए नहीं उपयोगी के लिए समर्पित में यह एक्सटेंशन बनाई गई हैं, लेकिन अगर आप में से किसी चाहते इसे पुन: करने के लिए)

extension CellPost { 
func setCollectionViewDataSourceDelegate<D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>>(dataSourceDelegate: D, forRow row: Int) { 

    collectionView.delegate = dataSourceDelegate 
    collectionView.dataSource = dataSourceDelegate 
    collectionView.tag = row 
    collectionView.setContentOffset(collectionView.contentOffset, animated:false) // Stops collection view if it was scrolling. 
    collectionView.reloadData() 
} 

var collectionViewOffset: CGFloat { 
    set { 
     collectionView.contentOffset.x = newValue 
    } 

    get { 
     return collectionView.contentOffset.x 
    } 
} 

किसी को भी इस कोड का उपयोग करना चाहते हैं, यह बहुत अच्छा काम करता है, लेकिन मैं एक tableView(... heightForRowAtIndexPath ...)

012 के साथ UITableView की ऊंचाई को हार्डकोड करने के लिए है

मैं UITableViewCell क्या यह अंदर के लिए अनुकूल (मैं नोक मैं सेल में डाल रहा हूं द्वारा भेजे गए सामग्री के आकार की गणना करने की कोशिश की, और फिर tableView(... heightForRowAtIndexPath ...) के लिए भेज लेकिन मैं यह काम नहीं कर सकते हैं बनाने के लिए सब कुछ करने की कोशिश की । मैंने ऑटो लेआउट का भी प्रयास किया लेकिन शायद मैं इसे गलत कर रहा हूं। मुझे यह भी लगता है कि समस्या उस हिस्से से आ सकती है जिसे मैंने अपने सेल में निब आयात किया था।

उपयोगकर्ता को UICollectionViewCell स्वाइप करने के बाद सेल का विस्तार करने का कोई तरीका नहीं है, क्या ऐसा होने पर कोई ईवेंट बनाने का कोई तरीका है? और शायद tableView(... heightForRowAtIndexPath ...) पर कॉल करें?

+0

शायद ही कभी इस तरह से इसका इस्तेमाल करते हैं, जब आप UICollectionView उपयोग करने का निर्णय, सबसे मामले में, UITableView उपयोग करने के लिए मुझे लगता है कि यह शामिल करने के लिए कोई जरूरत नहीं है। – childrenOurFuture

+0

@ जीन बैपटिस्ट, क्या स्वीकृत उत्तर आपकी समस्या का समाधान करता है? मैं बिल्कुल आपके कोड के समान ही कर रहा हूं लेकिन जब मैं UITableViewAutomaticDimension का उपयोग करता हूं तो मेरा संग्रह दृश्य बिल्कुल बाध्य नहीं होता है। क्या आप मुझे एक संकेत दे सकते हैं? –

उत्तर

1

जैसा कि मैं समझता हूं कि आपको टेबलव्यू सेल ऊंचाई समायोजित करने की आवश्यकता है। तो आप सेल ऊंचाई समायोजित करने के लिए autolayout का उपयोग कर सकते हैं। ViewDidLoad

self.tableView.estimatedRowHeight = 80 
self.tableView.rowHeight = UITableViewAutomaticDimension 

में

लिखें CellForRowAtIndexPath

में सेल रिटर्निंग
self.tableView.setNeedsLayout() 
self.tableView.layoutIfNeeded() 

आप autolayout के लिए लिंक मिल सकता है पहले। enter link description here

+1

यह काम नहीं करता है। निश्चित नहीं है कि यह क्यों स्वीकार किया जाता है। –

0

मेरी समस्या हल करने के लिए, मैं UITableView (cellHeight नाम) की प्रत्येक कोशिका की ऊंचाई स्टोर करने के लिए एक सरणी बनाया।

मैं सरणी से बना एक सरणी में प्रोग्राम की शुरुआत में प्रत्येक सेल की ऊंचाई की गणना भी करता हूं (नाम cellHeightForPost)। मुख्य सरणी का प्रतिनिधित्व सभी TableView cells, इसके अंदर प्रत्येक सरणी अद्यतन तालिका दृश्य हर मैं इसे अंदर collectionView बदलने के लिए, मैं fonction collectionView(... willDiplayCell...) इस्तेमाल किया करने के लिए 'collectionView'

की प्रत्येक कोशिका आदेश में की ऊंचाई का प्रतिनिधित्व करते हैं:

self.tableView.beginUpdates() 
cellHeight[collectionView.tag] = cellHeightForPost[collectionView.tag][indexPath.row] 
self.tableView.setNeedsLayout() 
self.tableView.layoutIfNeeded() 
self.tableView.endUpdates() 

मेरे UITableView आकारtableView(... heightForRowAtIndexPath) और tableView(... estimatedHeightForRowAtIndexPath) में परिभाषित किया गया है:

return cellHeight[indexPath.row] 

आकार collectionViewCells की सेट करने के लिए:

return CGSize(width: collectionView.frame.width, height: cellHeightForPost[collectionView.tag][indexPath.row]) 
संबंधित मुद्दे