TableView Footer View ব্যবহার করে টেবিলের নিচে বাটন যোগ করুন।
func initTableView() {
let tableView = UITableView()
tableView.frame = self.view.frame
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = .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.systemBlue.cgColor
cell.textLabel?.text = "cell at \(indexPath.row)"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 60 }
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return 100 }
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView()
footerView.backgroundColor = .systemYellow
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(.black, for: .normal)
button.backgroundColor = .systemGreen
footerView.addSubview(button)
return footerView
}
ফলাফল —
