के साथ देखें मैं तेजी से एक आईओएस ऐप बना रहा हूं, और मैं प्रोग्राम बनाने की कोशिश कर रहा हूं प्रोग्रामेटिक रूप से देखें। मैं संग्रह दृश्य के लिए शीर्षलेख के रूप में UICollectionReusableView के अपने उप-वर्ग का उपयोग करना चाहता हूं, क्योंकि मुझे हेडर में कुछ बटन और एक स्ट्रेच करने योग्य छवि चाहिए।आईओएस; प्रोग्रामेटिक रूप से संग्रह कस्टम हेडर
सुपरव्यू UICollectionReusableView है।
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
layout.headerReferenceSize = CGSizeMake(self.view.frame.width, 200)
someView = SupView(frame: CGRectMake(0, 0, view.frame.width, 200))
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell") // UICollectionReusableView
self.view.addSubview(collectionView)
}
मैं इस तरह, viewForSupplementaryElementOfKind
में पूरक देखें सम्मिलित करने के लिए कोशिश कर रहा हूँ, लेकिन मैं जब हेडर बनाने के लिए प्रयास में कोई त्रुटि हो रही है:
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
var reusableView : UICollectionReusableView? = nil
// Create header
if (kind == UICollectionElementKindSectionHeader) {
// Create Header
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell", forIndexPath: indexPath) as! SupView
headerView.frame = CGRectMake(0, 0, view.frame.width, 200)
reusableView = headerView
}
return reusableView!
}
त्रुटि let headerView = ...
में है और कहते हैं: "सिग्नल SIGABRT"
मुझे हेडर्व्यू को कैसे प्रारंभ करना चाहिए, इसलिए मैं अपने फ्लोलेआउट में इनपुट कर सकता हूं? शायद
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell")
साथ somewith लेकिन अगर मैं SupView स्तरीय यह मुझे त्रुटि देता है रजिस्टर करने के लिए प्रयास करें:
.../collectionViewPlay/ViewController.swift:32:24: Cannot invoke 'registerClass' with an argument list of type '(SupView!, forSupplementaryViewOfKind: String, withReuseIdentifier: String)'
किसी भी विचार?
संपादित करें:
उपवर्ग के कार्यान्वयन अनुरोध किया गया था:
import UIKit
class SupView: UICollectionReusableView {
//////////////////////////////////////////////////////////////////////////////
override init(frame: CGRect) {
super.init(frame: frame)
self.myCustomInit()
}
//////////////////////////////////////////////////////////////////////////////
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.myCustomInit()
}
func myCustomInit() {
print("hello there from SupView")
}
}
SupView – Shoaib