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

iOS swift UIImageView change image in tableView cell

我在 tableView 自定义 cell 中遇到了一个奇怪的问题。对于类似图像操作,我在 Custom cell 中编写了这些代码,称为 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:

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "FeedCell", for: indexPath) as! FeedViewCell

    return cell
}

但是正如您在 this 视频中看到的那样,当我触摸索引 0 中的“赞”按钮并更改图像时,索引 3 中的“赞”按钮也会更改图像。你们能告诉我我的错误吗?

谢谢

取一数组

let arr : NSMutableArray = NSMutableArray()

并且在 tableview 中使用按钮而不是图像并设置不同于按钮中的图像并设置你的图像而不是我的图像

 func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

        let btn = cell.viewWithTag(10) as! UIButton
        btn.addTarget(self, action: #selector(ViewController.connected(_:)), forControlEvents: .TouchUpInside)
        return cell
    }

    func connected(sender: UIButton){
        let buttonposition = sender.convertPoint(CGPointZero, toView: self.tblview)
        let index = self.tblview.indexPathForRowAtPoint(buttonposition)

        if !arr.containsObject((index?.row)!) {
            arr.addObject((index?.row)!)
            sender.setImage(UIImage(named: "ic_check.png"), forState: .Normal)
        }else
        {
            arr.removeObject((index?.row)!)
            sender.setImage(UIImage(named: "ic_uncheck.png"), forState: .Normal)
        }

    }

这就是为什么您要更改 imageView 中加载的图像,然后使用方法 tableView.dequeueReusableCell 重新使用具有更改图像的单元格。

在 iOS UITableViewUICollectionView 中应用 可重用单元格的概念 。他们不会为数组的每个元素创建一个单元格;他们只是创建 3-4 个单元格,然后他们会重复使用它,只是改变里面的内容。这是一件很棒的事情,因为它允许开发人员创建包含数百行的表而不会出现内存问题。

这些是 tableView(以及 collectionView)显示元素数组的步骤:

  1. 准备重用
  2. cellForRowAtIndexPath
  3. willDisplayCell

要解决您的问题,您只需检查 cellForRowAtIndexPath

中的类似内容

示例:

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "FeedCell", for: indexPath) as! FeedViewCell

    let model = arrayOfElements[indexPath.row]
    if model.isLiked {
        cell.like.image == UIImage(named: "like-btn-active")
    } else {
        cell.like.image == UIImage(named: "like-btn-active")
    }

    return cell
}

尝试编码其工作 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()
    }