Button state activates on wrong cells

前端 未结 3 393
一生所求
一生所求 2021-01-16 04:47

I added button into cell-s and added action so if user touches it then the state is \"Dislike\" and if user touches again the state is \"Like\". However, the state applies t

相关标签:
3条回答
  • 2021-01-16 05:14
     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!)
               // call your firebase method for update database 
            }else{
                  selectedindex.addObject(selectindex!)
              // call your firebase method for update database
            }
            self.tableview.reloadData()
        }
    

    Output :

    https://www.dropbox.com/s/ub7wln5y6hdw0sz/like%20button.mov?dl=0

    0 讨论(0)
  • 2021-01-16 05:16

    I think this issue is because of dequeuing your cell twice. you should try;

    func like(sender: UIButton){
        //your code ...
        let cell: FeedTableViewCell = self.tableViewAddress.cellForRowAtIndexPath(indexPath) as! FeedTableViewCell
        //Your code ...
    
    0 讨论(0)
  • 2021-01-16 05:29

    This is caused by reusing previous cells when scrolling and is the base mechanism of a table view.

    You need to reset the state of your button on every call to cellForRowAtIndexPath.

    Between let cell = ... and cell.starButton.addTarget you need to perform something like cell.starButton.deselect(), or .select(), based on the index path you're working on.

    0 讨论(0)
提交回复
热议问题