表视图单元格中带有 UIImageView 的 UITapGestureRecognizer 不工作 Swift
Posted
技术标签:
【中文标题】表视图单元格中带有 UIImageView 的 UITapGestureRecognizer 不工作 Swift【英文标题】:UITapGestureRecognizer with UIImageView in Table View Cell not working Swift 【发布时间】:2020-09-29 20:32:05 【问题描述】:在我的表视图控制器中,我从后端请求数据。然后将数据添加到一个数组中,并为请求中的每一行提供一个视图模型。
let getNotifications = GETNotificationsByUserID(user_id: user_id)
getNotifications.getNotifications notifications in
self.notifications = notifications.map notification in
let ret = NotificationViewModel()
ret.mainNotification = notification
return ret
class NotificationViewModel
var mainNotification: Notifications?
struct Notifications: Codable
var supporter_picture:String?
在同一个表视图控制器中,然后我将数组中的每个项目添加到表视图单元格中的变量中。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath) as! NotificationCell
cell.user_image.isUserInteractionEnabled = true
let item = self.notifications[indexPath.item]
cell.viewModel = item
return cell
从我的表格视图单元格变量的数据中下载图像并设置我的 UIImageView 图像。
UITableViewCell
class NotificationCell: UITableViewCell
var viewModel: NotificationViewModel?
didSet
if let item = viewModel
self.username.text = item.mainNotification?.supporter_username
item.supporterImageDownloader = DownloadImage()
item.supporterImageDownloader?.imageDidSet = [weak self] image in
self?.user_image.image = image
if let picture = item.mainNotification?.supporter_picture
item.supporterImageDownloader?.downloadImage(urlString: picture)
else
self.user_image.image = UIImage(named: "profile-placeholder-user")
UIImageView 是从惰性闭包变量创建的。惰性闭包变量还包含一个 UITapGestureRecognizer。
UITableViewCell
lazy var user_image: UIImageView =
let image = UIImageView()
let gesture = UITapGestureRecognizer()
gesture.addTarget(self, action: #selector(userImageClicked(_:)))
gesture.numberOfTapsRequired = 1
gesture.numberOfTouchesRequired = 1
image.addGestureRecognizer(gesture)
image.isUserInteractionEnabled = true
return image
()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.isUserInteractionEnabled = true
addSubview(user_image)
user_imageContraints()
func user_imageContraints()
user_image.translatesAutoresizingMaskIntoConstraints = false
user_image.topAnchor.constraint(equalTo:topAnchor, constant: 8).isActive = true
user_image.leadingAnchor.constraint(equalTo:leadingAnchor, constant: 8).isActive = true
user_image.heightAnchor.constraint(equalToConstant: 40).isActive = true
user_image.widthAnchor.constraint(equalToConstant: 40).isActive = true
@objc func userImageClicked(_ sender: UITapGestureRecognizer)
print("image clicked")
由于某种原因,当我的 UIImageView 被点击时,它什么也没做。
【问题讨论】:
怎么添加imageView来查看??? 它在表格视图单元格中。addSubview(user_image)
在我的初始化中
显示相关代码?约束?
cell.user_image.userInteractionEnabled = true,是关于?
@sekoyaz 没用
【参考方案1】:
问题出在这一行:
addSubview(user_image)
改成:
contentView.addSubview(user_image)
您绝不能将任何视图直接添加到单元格 - 只能添加到其 contentView
。
【讨论】:
这行得通。我必须更改一些约束以指向我的 contentView 并将我的子视图添加到我的 contentView 中。谢谢以上是关于表视图单元格中带有 UIImageView 的 UITapGestureRecognizer 不工作 Swift的主要内容,如果未能解决你的问题,请参考以下文章
选择 tableviewcell 时如何运行函数(例如,按下单元格时更新 UIImageView)
一个单元格中带有(多个数组 - 多个表)的 FILTER 函数 - Excel
为啥自调整大小的表格视图单元格中的 UIImageView 具有内容模式 AspectFit 的原始高度?