हाय सबको। मैंने 1 महीने पहले प्रोग्रामिंग सीखना शुरू किया था, इसलिए अगर मैं अपनी समस्या को अच्छी तरह से समझा नहीं तो कृपया अच्छा रहें :)स्विफ्ट: UICollectionView के आकार के आधार पर UITableViewCell ऊंचाई का विस्तार करें
मेरी परियोजना मुख्य UITableView
से बना है। UITableView
के प्रत्येक कक्ष में, मेरे पास UICollectionView
(क्षैतिज स्क्रॉलिंग पर) है।
प्रत्येक UICollectionViewCell
की चौड़ाई पूरे UITableViewCell
के समान है। मेरी पहली समस्या UITableViewCell
की ऊंचाई का आकार (जो UICollectionView
के आकार और इसके शीर्ष पर सामग्री का आकार) पर निर्भर करेगा।
यह स्वचालित रूप से किया जा सकता है। वास्तव में, 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 ...)
UITableView
की ऊंचाई को हार्डकोड करने के लिए है
मैं UITableViewCell
क्या यह अंदर के लिए अनुकूल (मैं नोक मैं सेल में डाल रहा हूं द्वारा भेजे गए सामग्री के आकार की गणना करने की कोशिश की, और फिर tableView(... heightForRowAtIndexPath ...)
के लिए भेज लेकिन मैं यह काम नहीं कर सकते हैं बनाने के लिए सब कुछ करने की कोशिश की । मैंने ऑटो लेआउट का भी प्रयास किया लेकिन शायद मैं इसे गलत कर रहा हूं। मुझे यह भी लगता है कि समस्या उस हिस्से से आ सकती है जिसे मैंने अपने सेल में निब आयात किया था।
उपयोगकर्ता को UICollectionViewCell
स्वाइप करने के बाद सेल का विस्तार करने का कोई तरीका नहीं है, क्या ऐसा होने पर कोई ईवेंट बनाने का कोई तरीका है? और शायद tableView(... heightForRowAtIndexPath ...)
पर कॉल करें?
शायद ही कभी इस तरह से इसका इस्तेमाल करते हैं, जब आप UICollectionView उपयोग करने का निर्णय, सबसे मामले में, UITableView उपयोग करने के लिए मुझे लगता है कि यह शामिल करने के लिए कोई जरूरत नहीं है। – childrenOurFuture
@ जीन बैपटिस्ट, क्या स्वीकृत उत्तर आपकी समस्या का समाधान करता है? मैं बिल्कुल आपके कोड के समान ही कर रहा हूं लेकिन जब मैं UITableViewAutomaticDimension का उपयोग करता हूं तो मेरा संग्रह दृश्य बिल्कुल बाध्य नहीं होता है। क्या आप मुझे एक संकेत दे सकते हैं? –