簡體   English   中英

CollectionView 內的按鈕重復第一個單元格信息

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

我正在使用 swift 從我的 Wordpress 網站的 json 創建圖像應用程序,我已經創建了 CollectionView 和每個單元格顯示圖像並且工作正常,但我想在每個單元格中添加顯示評論,以顯示每個帖子的確切評論當我點擊它時,它會顯示收藏視圖的第一個帖子的評論。 這是我顯示評論和可點擊按鈕的代碼。

 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
}

這是按鈕單擊的代碼

@objc func comment(_ sender: Any) {

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


}

我希望你能理解我的問題,謝謝

您已將indexPath聲明為全局變量,並且它的值是NSIndexPath(row: 0, section: 0)如您所說。

comments(_:) function 中你使用indexPath.row但這row0所以它是第一個帖子的評論。

您不需要將tapgesture設置為cell的按鈕。


homeViewCell ,您應該為按鈕創建IBAction並在觸發時調用關閉 ->

class homeViewCell: UICollectionViewCell {

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

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

像這樣在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
 }
}

刪除以下內容;

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

  • @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