简体   繁体   English

CollectionView 内的按钮重复第一个单元格信息

[英]Button inside the CollectionView repeating the first cell information

i am creating images app from my Wordpress website's json using swift, i have created CollectionView and every cell displaying images and working fine but i want to add show comment in every cell for each post, its showing the exact comments for every post/cell but when i click on it it shows the comments of very first post of collection view.我正在使用 swift 从我的 Wordpress 网站的 json 创建图像应用程序,我已经创建了 CollectionView 和每个单元格显示图像并且工作正常,但我想在每个单元格中添加显示评论,以显示每个帖子的确切评论当我点击它时,它会显示收藏视图的第一个帖子的评论。 this is my code to show comments and for clickable button.这是我显示评论和可点击按钮的代码。

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

    let url = dataArray[indexPath.row]["thumbnail_images"]["medium"]["url"].stringValue
    cell.imageArticle.sd_setImage(with: NSURL(string: url) as URL?, placeholderImage:UIImage(named: "empty.png"))
     cell.nametag.text = String(htmlEncodedString:dataArray[indexPath.row]["tags"][0]["title"].stringValue)
    cell.nametagg.text =  String(htmlEncodedString:dataArray[indexPath.row]["categories"][0]["title"].stringValue)



    cell.fbButton.addTarget(self, action: #selector(homeController.fbClick), for: .touchUpInside)

    cell.commentButton.addTarget(self, action: #selector(homeController.comment), for: .touchUpInside)
    cell.commentButton.setTitle("\(dataArray[indexPath.row]["comment_count"].stringValue) comments", for: .normal)
    cell.leaveComment.addTarget(self, action: #selector(homeController.leavecomment), for: .touchUpInside)




   cell.contentline.text = String(htmlEncodedString:dataArray[indexPath.row]["excerpt"].stringValue)





 return cell
}

and this is the code for button click这是按钮单击的代码

@objc func comment(_ sender: Any) {

  let vc = CommentViewController()
    vc.dataArray = dataArray[indexPath.row]["comments"].array
    self.navigationController!.pushViewController(vc, animated: true)


}

i hope you understand my question, thanks我希望你能理解我的问题,谢谢

You've declared the indexPath as a global variable and it's value is NSIndexPath(row: 0, section: 0) as you said.您已将indexPath声明为全局变量,并且它的值是NSIndexPath(row: 0, section: 0)如您所说。

in comments(_:) function you've used indexPath.row but this row is 0 so it is first post's comments.comments(_:) function 中你使用indexPath.row但这row0所以它是第一个帖子的评论。

You don't need to set tapgesture to cell 's button.您不需要将tapgesture设置为cell的按钮。


In homeViewCell you should create IBAction for the button and call closure when it triggered ->homeViewCell ,您应该为按钮创建IBAction并在触发时调用关闭 ->

class homeViewCell: UICollectionViewCell {

    public var didTapComment: (() -> Void)?

    @IBAction func didTapCommentButton(_ sender: UIButton) {
        didTapComment?()
    }
}

Set didTapComment 's action in cellForItemAt like this ->像这样在cellForItemAt中设置didTapComment的动作 ->

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

    cell.didTapComment = { [weak self] in
        let vc = CommentViewController()
        vc.dataArray = dataArray[indexPath.row]["comments"].array
        self?.navigationController!.pushViewController(vc, animated: true)
    }

    let url = dataArray[indexPath.row]["thumbnail_images"]["medium"]["url"].stringValue

    cell.imageArticle.sd_setImage(with: NSURL(string: url) as URL?, placeholderImage:UIImage(named: "empty.png"))
    cell.nametag.text = String(htmlEncodedString:dataArray[indexPath.row]["tags"][0]["title"].stringValue)
    cell.nametagg.text =  String(htmlEncodedString:dataArray[indexPath.row]["categories"][0]["title"].stringValue)

    cell.fbButton.addTarget(self, action: #selector(homeController.fbClick), for: .touchUpInside)

    cell.commentButton.setTitle("\(dataArray[indexPath.row]["comment_count"].stringValue) comments", for: .normal)
    cell.leaveComment.addTarget(self, action: #selector(homeController.leavecomment), for: .touchUpInside)

    cell.contentline.text = String(htmlEncodedString:dataArray[indexPath.row]["excerpt"].stringValue)

    return cell
 }
}

Remove followings;删除以下内容;

  • cell.commentButton.addTarget(self, action: #selector(homeController.comment), for: .touchUpInside) line from cellForItemAt cell.commentButton.addTarget(self, action: #selector(homeController.comment), for: .touchUpInside)来自cellForItemAt的行

  • @objc func comment(_ sender: Any) function @objc func comment(_ sender: Any) function

  • let indexPath = NSIndexPath(row: 0, section: 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM