0

我希望弹出用户的图像(没有操作选项),然后在外面触摸时将其关闭。

假设表格视图由 2 个 UI 元素(一个按钮和一个文本)和一张图片组成。您将如何设置仅为图片的弹出上下文菜单 - immy?

public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as!  ViewControllerTableViewCell
    let immy = cell.viewWithTag(1) as! UIImageView
    let person: Userx = people[indexPath.row]
    let button = cell.viewWithTag(3) as! UIButton
    cell.lblName.text = person.Education
    cell.postID = self.people[indexPath.row].postID
    if let PhotoPosts = person.PhotoPosts {
        let url = URL(string: PhotoPosts)
        print(PhotoPosts, "sttdb")
        immy.sd_setImage(with: url)
    }
    return cell
}

extension homepage {
    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil)
        return configuration
    }
}

下面是在第一条评论之后添加的 ViewControlleTableViewCell。我对评论的回复还解释了它在 main.storyboard 中的外观。

class ViewControllerTableViewCell: UITableViewCell {    

    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var immy: UIImageView!
    @IBOutlet weak var lblgenre1: UILabel!    
    @IBOutlet weak var lblName: UILabel!
    @IBOutlet weak var lblName1: UILabel!
    @IBOutlet weak var likeLabel: UILabel!

    var postID: String!
    var caption1: String!
    var keyToPost: String!
    var latestLocation1: [String: Double]?
    let databaseRef = Database.database().reference()
    var refArtists: DatabaseReference!

    let profileImageView: UIImageView = {
        let imageView = UIImageView()
        
        return imageView
    } ()
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    @IBAction func button1(_ sender: Any) {

    }
    @IBAction func button2NBTHISISTORPREVENTSEGUE(_ sender: Any) {

    }
}

在主页中:

 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
4

1 回答 1

0

您需要的是在您的 中启用用户交互UIImageView,使您ViewControllerTableViewCell符合UIContextMenuInteractionDelegate,创建一个UIContextMenuInteraction并将您的单元格设置为委托。然后将迭代添加到您的图像视图中。不要忘记contextMenuInteraction从您的主页扩展中删除该方法。

您的ViewControllerTableViewCellcontextMenuInteraction 交互实现应如下所示:

import UIKit

class ViewControllerTableViewCell: UITableViewCell, UIContextMenuInteractionDelegate {
    @IBOutlet weak var immy: UIImageView!
    override func awakeFromNib() {
        super.awakeFromNib()
        immy.isUserInteractionEnabled = true
        immy.addInteraction(UIContextMenuInteraction(delegate: self))
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        UIContextMenuConfiguration(identifier: nil, previewProvider: nil)  { _ in
            let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up")) { _ in
               // share code
            }
            return UIMenu(title: "Profile Picture Menu", children: [share])
        }
    }
}
于 2020-07-15T01:11:06.953 回答