【问题标题】:UICollectionViewCell Protocol is not workingUICollectionViewCell 协议不起作用
【发布时间】:2019-05-15 09:42:31
【问题描述】:

我的 CellClass 中有一个协议,用于检测函数“editCommentInFeed”。我已经在我的单元格中创建了一个“editButton”,并使用“editCommentField”定义了处理程序,该处理程序正在调用每个委托函数并提供 2 个参数。这 2 个参数很重要,我需要来自单元格的这些信息来检测将来提供的操作。

例如,当按钮在单元格内被按下时,我试图打印一些东西。但是连这个Statement都没有执行。


//This is the Cell Class

protocol CommentFeedProtocol {
    func editCommentInFeed(cell: CommentCell, comment: Comment)
}

class CommentCell: BaseCell {

      var delegate: CommentFeedProtocol?

      let editButton: UIButton = {

        let editbttn = UIButton(type: UIButton.ButtonType.system)
        editbttn.setImage(UIImage(named: "editButton"), for: UIControl.State.normal)
        editbttn.addTarget(self, action: #selector(editCommentField), for: UIControl.Event.touchUpInside)

        return editbttn
    }()


    override func setUpCell() {
        super.setUpCell()

        backgroundColor = UIColor.white


        setUpCommentCell()

    }

    fileprivate func setUpCommentCell(){

        addSubview(profileImageView)
        addSubview(editButton)
        addSubview(commentTextView)
        addSubview(seperator)

        profileImageView.anchor(top: topAnchor, left: leftAnchor, bottom: nil, right: nil, paddingTop: 8, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 40, height: 40)
        profileImageView.layer.cornerRadius = 40/2

        editButton.anchor(top: topAnchor, left: nil, bottom: bottomAnchor, right: rightAnchor, paddingTop: 5, paddingLeft: 0, paddingBottom: 5, paddingRight: 10, width: 45, height: 0)

        commentTextView.anchor(top: topAnchor, left: profileImageView.rightAnchor, bottom: bottomAnchor, right: editButton.leftAnchor, paddingTop: 4, paddingLeft: 4, paddingBottom: 4, paddingRight: 4, width: 0, height: 0)

        seperator.anchor(top: bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 5, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 1.5)


    }


    /*Objecthandler*/

    @objc func editCommentField(){
        guard let comment = comment else { return}
        delegate?.editCommentInFeed(cell: self, comment: comment)
    }


// This is the CollectionViewController
// As you can see I am also add the Protocol to the class 

class CommentsController: UICollectionViewController, UICollectionViewDelegateFlowLayout, CommentFeedProtocol {

override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "Kommentare"

        postButton.imageView?.alpha = 0.5
        postButton.isEnabled = false


        collectionView.keyboardDismissMode = .interactive
        collectionView.register(CommentCell.self, forCellWithReuseIdentifier: commentCell)
        collectionView.alwaysBounceVertical = true

        self.collectionView.backgroundColor = UIColor.white

        fetchComments()

    }


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: commentCell, for: indexPath) as! CommentCell


        cell.comment = comments[indexPath.item]
        cell.delegate = self

        return cell
    }

// Here is the Delegate Function

    func editCommentInFeed(cell: CommentCell, comment: Comment) {
        print("Button was Pressed")
    }


您可以在示例中看到我正在将协议添加到类并在类中调用函数。我没有收到错误消息。 Button 仅在单击时轻轻悬停,但不执行任何操作?!

【问题讨论】:

标签: swift xcode uicollectionview uicollectionviewcell swift-protocols


【解决方案1】:

似乎委托未设置为您的 ViewController。 在您的cellForItemAt 函数中,您需要设置cell.delegate = self。 因此,当您的单元格调用 delegate.editCommentInFeed 时,它实际上会调用您的 viewController 的 editCommentInFeed 实现的函数。

【讨论】:

  • 您好,感谢您的回答!我很抱歉上面的小复制错误。 cell.delegate = self 已经在我的 cellForItemAt 中,问题是它仍然无法正常工作,所以我没有执行我的示例 print 语句。 :(
【解决方案2】:

你还没有在cellForItemAt中设置代理:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: commentCell, for: indexPath) as! CommentCell
    cell.comment = comments[indexPath.item]
    cell.delegate = self. // You forgot to add this line

    return cell
}

您可以在这里阅读更多信息:

您还可以搜索有关它的精彩教程。

【讨论】:

  • 首先我要感谢您的回答,但这只是一个复制错误,我现在已经更正了。委托已在cellForItemAt 中设置,但仍未触发该函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 2011-11-01
  • 1970-01-01
  • 2017-11-08
  • 1970-01-01
  • 1970-01-01
  • 2015-12-09
相关资源
最近更新 更多