我没有收到任何感动。它会在iPhone屏幕上显示选择内容,但不会执行方法didSelectRow
。
usersTV.register(UserCell.self, forCellReuseIdentifier: "userCell")
usersTV.dataSource = self
usersTV.delegate = self
usersTV.backgroundColor = .clear
usersTV.separatorColor = .white
usersTV.translatesAutoresizingMaskIntoConstraints = false
usersTV.isHidden = true
usersTV.tableFooterView = UIView()
usersTV.isUserInteractionEnabled = true
usersTV.isEditing = false
usersTV.allowsSelection = true
usersTV.allowsSelectionDuringEditing = false
usersTV.allowsMultipleSelection = false
usersTV.allowsMultipleSelectionDuringEditing = false
usersTV.delaysContentTouches = false
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
所有内容均以编程方式编写。 单元格是自定义的,带有3个标签并添加了约束。
可能是什么问题?
更新:
class UserCell : UITableViewCell{
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
let viewsDict = ["userName":userName,"field1":field1,"field2":field2]
contentView.superview?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[userName(120)]-8-[field1]-8-[field2(120)]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDict))
contentView.superview?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[userName]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDict))
contentView.superview?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[field1]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDict))
contentView.superview?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[field2]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDict))
更新: 仅当您用两根手指点击时,didSelectRow方法才有效...
答案 0 :(得分:1)
TableViewCell is not clickable with one finger tap, but it is with two fingers
这个问题的答案帮助了我。我在主视图上设置了GestureRecognizer。当我删除它时,问题就解决了。
答案 1 :(得分:0)
我认为这段代码会对您有所帮助。否则,标签约束可能会出现问题。
@IBOutlet weak var UserTV: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
UserTV.register(UserCell.self, forCellReuseIdentifier: "userCell")
UserTV.dataSource = self
UserTV.delegate = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let vc :UserCell = self.UserTV.dequeueReusableCell(withIdentifier: "userCell") as! UserCell
vc.textLabel?.text = "Index: \(indexPath.row)"
vc.backgroundColor = UIColor.red
return vc
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Slected \(indexPath.row)")
}
答案 2 :(得分:0)
首先,这是我对您的代码的评论:
我对您的建议
SampleTableViewController类:UIViewController,UITableViewDataSource,UITableViewDelegate {
// MARK: - Variables
lazy private var listTableView: UITableView = {
let tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.delegate = self
tableView.dataSource = self
return tableView
}()
// MARK: - View LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
initialSetup()
}
// MARK: - Private Methods
private func initialSetup() {
view.backgroundColor = .white
view.addSubview(listTableView)
listTableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
listTableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
listTableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
listTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
listTableView.register(UITableViewCell.self, forCellReuseIdentifier: "listCell")
}
// MARK: - TableView Methods
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "listCell", for: indexPath)
cell.textLabel?.text = "Table Row - \(indexPath.row)"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(#function)
}