टेबल व्यू फुटर में सबमिट बटन जोड़ना
टेबल व्यू के अंत में एक सबमिट बटन जोड़ने के लिए, हम टेबल व्यू फुटर्स का उपयोग कर सकते हैं। आइए इसे एक उदाहरण की मदद से देखें जहां हम अपने टेबल में एक फुटर व्यू जोड़ेंगे, और टेबल के अंदर, हम टेबल व्यू के नीचे बटन जोड़ने के लिए कोड जोड़ेंगे।
टेबल व्यू इनिशियलाइज़ करना
पहले एक नया प्रोजेक्ट बनाएं, फिर व्यू कंट्रोलर के अंदर निम्नलिखित कोड जोड़ें जो टेबल को इनिशियलाइज़ करेगा, एक सेक्शन और कुछ पंक्तियां टेबल में जोड़ेगा।
func initTableView() {
let tableView = UITableView()
tableView.frame = self.view.frame
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.lightGray
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(tableView)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.layer.backgroundColor = UIColor.blue.cgColor
cell?.textLabel?.text = "cell at \(indexPath.row)"
return cell!
}
अब, पहली फंक्शन initTableView() को अपने व्यू कंट्रोलर के viewDidLoad या viewDidAppear मेथड के अंदर कॉल करें।
अब निम्नलिखित कोड जोड़ें जो टेबल को उसकी पंक्तियों और फुटर को कुछ ऊंचाई देने के लिए कहेगा।
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 100
}
सुनिश्चित करें कि आपने अपनी क्लास को UITableViewDataSource और UITableViewDelegate के अनुरूप बनाया है, अन्यथा ऊपर के मेथड्स एरर के रूप में दिखाई देंगे।
फुटर व्यू और बटन जोड़ना
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView()
footerView.backgroundColor = UIColor.yellow
footerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 100)
let button = UIButton()
button.frame = CGRect(x: 20, y: 10, width: 300, height: 50)
button.setTitle("CustomButton", for: .normal)
button.setTitleColor(UIColor.black, for: .normal)
button.backgroundColor = UIColor.green
footerView.addSubview(button)
return footerView
}
जब हम उपरोक्त कोड को अपने डिवाइस पर चलाते हैं, तो नीचे वह परिणाम उत्पन्न होता है। आप बटन में कस्टम एक्शन जोड़ सकते हैं और आवश्यकता के अनुसार इसे कस्टमाइज़ कर सकते हैं।
