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

时间:2016-10-15 10:03:55

标签: ios swift uitableview uiimageview uitapgesturerecognizer

我在tableView自定义cell中遇到了一个奇怪的问题。对于类似图片操作,我在自定义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中的like按钮并更改图像时,索引3中的like按钮也会更改图像。你能告诉我我的错误吗?

感谢

3 个答案:

答案 0 :(得分:3)

尝试使用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()
    }

答案 1 :(得分:0)

拿一个阵列

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)
        }

    }

答案 2 :(得分:0)

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

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

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

  1. prepareForReuse
  2. 的cellForRowAtIndexPath
  3. willDisplayCell
  4. 要解决您的问题,您只需在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
    }