无法单击 UITableViewCell 中的按钮?
Can't click button inside UITableViewCell?
我正在使用故事板创建一个 UITableView,加载一些数据以推荐用户关注,并在单元格内包含一个关注按钮。
唯一的问题是,我无法单击单元格内的关注按钮。
这是我的 ViewController class 中 TableView 的代码(它不是表ViewController,而是包含 UITableViewDelegate 和 UITableViewDataSource 的 UIViewController) :
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return followRecommendations.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let followCell = tableView.dequeueReusableCell(withIdentifier: "followCell", for: indexPath) as! FollowRecommendationTableViewCell
followCell.followButton.tag = indexPath.row
followCell.followButton.addTarget(self, action: #selector(self.buttonTapped), for: UIControlEvents.touchUpInside)
followCell.followRecommendationName.text = followRecommendations[indexPath.row].suggestedUserName
followCell.followRecommendationInterests.text = followRecommendations[indexPath.row].suggestedUserTasteString
let imageUrl = followRecommendations[indexPath.row].suggestedUserImage
followCell.followRecomendationImage.downloadedFrom(link: imageUrl)
return followCell
}
func buttonTapped() {
print("Tapped")
}
这里是 TableViewCell 的代码 class。
class FollowRecommendationTableViewCell: UITableViewCell {
@IBOutlet weak var followRecomendationImage: UIImageView!
@IBOutlet weak var followRecommendationName: UILabel!
@IBOutlet weak var followRecommendationInterests: UILabel!
@IBOutlet weak var followButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
我以前在 Whosebug 上看到过这个问题,但我无法解决问题。以下是我尝试过的一些方法:
- TableView、单元格、内容视图和按钮的 enabling/disabling 用户交互的多种变体。
- 使用以下代码将按钮移动到内容视图的前面:
followCell.bringSubview(toFront: followCell.followButton)
- 我已经尝试了本教程中显示的 delegate/protocol 方法:http://candycode.io/how-to-properly-do-buttons-in-table-view-cells/
我似乎无法让它工作。
有没有其他人遇到过这个问题并找到了解决方案?
提前致谢!
我检查过 - 你的代码工作正常。您可能没有为单元格上的按钮设置 IBOutlet,无论是在 Storyboard 中(还是在单独的 .xib 文件中,如果您使用的话)。打开故事板并将单元格的出口与按钮匹配,如下所示:
我正在使用故事板创建一个 UITableView,加载一些数据以推荐用户关注,并在单元格内包含一个关注按钮。
唯一的问题是,我无法单击单元格内的关注按钮。
这是我的 ViewController class 中 TableView 的代码(它不是表ViewController,而是包含 UITableViewDelegate 和 UITableViewDataSource 的 UIViewController) :
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return followRecommendations.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let followCell = tableView.dequeueReusableCell(withIdentifier: "followCell", for: indexPath) as! FollowRecommendationTableViewCell
followCell.followButton.tag = indexPath.row
followCell.followButton.addTarget(self, action: #selector(self.buttonTapped), for: UIControlEvents.touchUpInside)
followCell.followRecommendationName.text = followRecommendations[indexPath.row].suggestedUserName
followCell.followRecommendationInterests.text = followRecommendations[indexPath.row].suggestedUserTasteString
let imageUrl = followRecommendations[indexPath.row].suggestedUserImage
followCell.followRecomendationImage.downloadedFrom(link: imageUrl)
return followCell
}
func buttonTapped() {
print("Tapped")
}
这里是 TableViewCell 的代码 class。
class FollowRecommendationTableViewCell: UITableViewCell {
@IBOutlet weak var followRecomendationImage: UIImageView!
@IBOutlet weak var followRecommendationName: UILabel!
@IBOutlet weak var followRecommendationInterests: UILabel!
@IBOutlet weak var followButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
我以前在 Whosebug 上看到过这个问题,但我无法解决问题。以下是我尝试过的一些方法:
- TableView、单元格、内容视图和按钮的 enabling/disabling 用户交互的多种变体。
- 使用以下代码将按钮移动到内容视图的前面:
followCell.bringSubview(toFront: followCell.followButton)
- 我已经尝试了本教程中显示的 delegate/protocol 方法:http://candycode.io/how-to-properly-do-buttons-in-table-view-cells/
我似乎无法让它工作。
有没有其他人遇到过这个问题并找到了解决方案?
提前致谢!
我检查过 - 你的代码工作正常。您可能没有为单元格上的按钮设置 IBOutlet,无论是在 Storyboard 中(还是在单独的 .xib 文件中,如果您使用的话)。打开故事板并将单元格的出口与按钮匹配,如下所示: