tableView-未调用didSelectRowAtIndexPath

时间:2018-09-29 14:25:18

标签: swift uitableview uiview

我有一个UITableView,它嵌入在UIView中,用作下拉视图(见图)。但是,它不响应单元格用户的触摸(使用didSelectRowAtIndexPath函数进行测试)。请参见下面的代码。

此方法的工作原理是,下拉视图是按钮的子视图(位于静态UITableViewCell中),因此当触摸按钮时,下拉视图的高度将从{{1 }}到0(单元格的高度也会改变)。

我检查了视图层次结构,没有任何东西阻止视图注册触摸。

下拉视图自定义类

88

在父级class GenderDropDownView: UIView, UITableViewDelegate, UITableViewDataSource { var genders = ["male.", "female."] var tableView = UITableView() override init(frame: CGRect) { super.init(frame: frame) self.layer.cornerRadius = 6 } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func layoutSubviews() { tableView.delegate = self tableView.dataSource = self self.addSubview(tableView) // Table View constraints tableView.translatesAutoresizingMaskIntoConstraints = false tableView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true tableView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true tableView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true tableView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true // Table view design tableView.backgroundColor = .clear tableView.separatorStyle = .none tableView.allowsSelection = true tableView.isUserInteractionEnabled = true } 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 = UITableViewCell() cell.textLabel?.text = genders[indexPath.row] cell.textLabel?.font = UIFont.systemFont(ofSize: 16, weight: .medium) cell.textLabel?.textColor = .white cell.textLabel?.textAlignment = .center cell.backgroundColor = .clear cell.selectionStyle = .none cell.isUserInteractionEnabled = true return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print(genders[indexPath.row]) } } 中配置下拉视图

UITableViewController

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

  

此方法的工作原理是下拉视图是按钮的子视图

但是下拉视图显示在按钮外部。因此,它不在其超级视图的范围之内,这意味着它无法响应触摸。

相关问题