【发布时间】:2020-02-29 00:01:46
【问题描述】:
我在 tableView 的每个部分的末尾都有一个名为 addSet 的按钮,它用作 footerView,它应该告诉 UITableViewController 何时按下它以及在哪个部分。我的自定义表格视图单元格代码如下
import UIKit
class FooterTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
var footerDelegate:FooterTableViewCellDelegate?
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func addSetIsPressed(_ sender: AnyObject) {
print("Add Set is pressed")
footerDelegate?.didAddSetIsPressed(cell:self)
}
}
protocol FooterTableViewCellDelegate {
func didAddSetIsPressed(cell:FooterTableViewCell)
}
在我的 TableViewController 中,我是这样实现的
func didAddSetIsPressed(cell: FooterTableViewCell) {
let indexPath = tableView.indexPath(for: cell)
print("Index path is \(indexPath)")
}
我想在用户点击我的按钮时获取 indexPath(具体的部分),但它总是返回 nil。我究竟做错了什么?
把事情放在上下文中。我使用这个单元格作为footerView,所以单元格是这样实现的
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "footerCell") as! FooterTableViewCell
cell.footerDelegate = self
return cell
}
所以它没有像通常那样在 indexPath 的 cellForRow 中实现
提前致谢。
【问题讨论】:
标签: ios swift xcode uitableview