iOS swift UIImageView在tableView单元格中更改图像

编程入门 行业动态 更新时间:2024-10-28 13:26:24
本文介绍了iOS swift UIImageView在tableView单元格中更改图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 tableView 自定义单元格中有一个奇怪的问题。对于类似Image动作,我在Custom cell 中编写这些代码,名为 FeedViewCell :

I have a strange problem in tableView Custom cell. for like Image action I write these code in Custom cell called FeedViewCell:

self.like.isUserInteractionEnabled = true let CommenttapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(likehandleTap)) self.like.addGestureRecognizer(CommenttapGestureRecognizer) func likehandleTap(_ sender: UITapGestureRecognizer) { if self.like.image == UIImage(named: "like-btn-inactive") { self.like.image = UIImage(named: "like-btn-active") } else { self.like.image = UIImage(named: "like-btn-inactive") } }

和TableViewController :

and TableViewController:

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "FeedCell", for: indexPath) as! FeedViewCell return cell }

但正如您所见触摸时此视频索引0中的类似按钮并改变图像,索引3中的类似按钮也改变图像。你能告诉我我的错误吗?

but as you see in this video when I touch the like button in index 0 and change the image, the like button in index 3 change image also. can you guys tell me my mistake please?

谢谢

推荐答案

尝试使用100%的代码

var selectindex : Int? var selectedindex : NSMutableArray = NSMutableArray() @IBOutlet var tableview: UITableView! func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("LikeCell", forIndexPath: indexPath) let like: UIButton = (cell.viewWithTag(2) as! UIButton) let comment: UIButton = (cell.viewWithTag(3) as! UIButton) if selectedindex.containsObject(indexPath.row) { like.setBackgroundImage(UIImage.init(named: "like.png"), forState: .Normal) }else{ like.setBackgroundImage(UIImage.init(named: "like (1).png"), forState: .Normal) } comment.setBackgroundImage(UIImage(named: "chat.png"), forState: UIControlState.Normal) like.addTarget(self, action: #selector(self.CloseMethod(_:event:)), forControlEvents: .TouchDown) comment.addTarget(self, action: #selector(self.CloseMethod1(_:event:)), forControlEvents: .TouchDown) return cell } @IBAction func CloseMethod(sender: UIButton, event: AnyObject) { let touches = event.allTouches()! let touch = touches.first! let currentTouchPosition = touch.locationInView(self.tableview) let indexPath = self.tableview.indexPathForRowAtPoint(currentTouchPosition)! selectindex = indexPath.row if selectedindex.containsObject(selectindex!) { selectedindex.removeObject(selectindex!) }else{ selectedindex.addObject(selectindex!) } self.tableview.reloadData() }

更多推荐

iOS swift UIImageView在tableView单元格中更改图像

本文发布于:2023-11-26 20:25:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635071.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元格   图像   swift   iOS   tableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!