[英]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
但这row
是0
所以它是第一个帖子的评论。
您不需要将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.