UITableView';s didSelectRow方法不工作
我没有收到抚摸。它在iPhone屏幕上显示所选内容,但不执行方法UITableView';s didSelectRow方法不工作,uitableview,swift4,didselectrowatindexpath,Uitableview,Swift4,Didselectrowatindexpath,我没有收到抚摸。它在iPhone屏幕上显示所选内容,但不执行方法didSelectRow usersTV.register(UserCell.self, forCellReuseIdentifier: "userCell") usersTV.dataSource = self usersTV.delegate = self usersTV.backgroundColor = .clear usersTV.separatorColor = .white
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个标签和添加的约束
@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)")
}
有什么问题吗
更新:
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方法只有在您用两个手指点击时才有效…我认为这段代码会对您有所帮助。否则,标签约束可能会有问题
@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)")
}
首先,以下是我对您的代码的评论:
- 这项简单的任务不需要太复杂
- 不要忘记正确设置委托和数据源
- 无需使用可视化格式的方式来设置约束,使用锚定
- 始终尝试使用UICollectionView而不是UITableView李>
// 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)
}
这个问题的答案对我很有帮助。我在主视图上设置了GestureRecognizer。当我删除它时,问题会得到解决。约束如何与单元格上的触摸相连接?它们如何产生问题?我更新了问题。请检查!我必须使用我的代码,因为我正在游戏场景中显示表视图。约束是我设置的n CustomClassCell:UITableViewCell{override init(style:UITableViewCellStyle,reuseIdentifier:String?{constraints}}我对tableView约束没有问题。显然,问题在于单元格的contentView约束。您能分享您想要如何显示单元格的图像吗?
H:|-8-[userName(120)]-8-[field1]-8-[field2(120)]-8-|
前导,用户名120宽度,8空格,字段1,8空格,字段2 120宽度,空格8,尾随