Reusing cell doesn't work well - TableView

前端 未结 6 1866
余生分开走
余生分开走 2021-01-27 12:35

I have a problem about my cell\'s button. In my tableView each row is composed by: an image, some labels and a button. The button has a checkmark image. When it is clicked, the

相关标签:
6条回答
  • 2021-01-27 12:56

    UITableViewCell is reusable. You can't store state of view in cell. You should setup cell in

    func tableView(UITableView, cellForRowAt: IndexPath)  
    

    method of your data source

    The easiest way to achieve that is to implement

    func tableView(UITableView, didSelectRowAt: IndexPath) 
    func tableView(UITableView, didDeselectRowAt: IndexPath)
    

    methods of UITableViewDelegate

    Then you can add/remove indexPath to some array in these methods and in cellForRowAtIndexPath setup cell.

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCellWithIdentifier("YourTableViewCell") as! YourTableViewCell
    
            if array.contains(indexPath) {
             cell.checkBook.image = UIImage(named: "check")
            } else {
             cell.checkBook.image = UIImage(named: "uncheck")
            }
    
            return cell
    }  
    
    0 讨论(0)
  • 2021-01-27 13:03

    I faced this problem recently, and did not find much about it. What solve, after much searching, was to use:

    override func prepareForReuse() {
    
        btnAdd.setImage(nil, for: .normal) //here I use to change to none image
        super.prepareForReuse()
    } 
    

    just put this method inside your custom UITableViewCell, and set which item you want to realese stats.

    0 讨论(0)
  • 2021-01-27 13:05

    From the information in your post, this looks like a cell reuse issue. The problem is that the tableView reuses the cells rather than creating new ones, to maintain performance. If you haven't reset the cell's state, the reused cell will be remain configured in the old state.

    For a quick fix, you can implement the prepareForReuse method on UITableViewCell.

    However, you'll need to store which cell is 'checked' in your view controller if you want the checkbox to be selected after scrolling the tableView. You can store this yourself, or use the tableView's didSelectRowAtIndexPath method.

    0 讨论(0)
  • 2021-01-27 13:05

    Try my code . here selectindex is use for get selected cell index and selectedindex is NSMutableArray that i store all selected cell value.

    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)// like button
         let comment: UIButton = (cell.viewWithTag(3) as! UIButton) // comment button
         comment.setBackgroundImage(UIImage(named: "chat.png"), forState: UIControlState.Normal) // comment button set
         like.addTarget(self, action: #selector(self.CloseMethod(_:event:)), forControlEvents: .TouchDown)
         comment.addTarget(self, action: #selector(self.CloseMethod1(_:event:)), forControlEvents: .TouchDown)
    
         return cell
    }
    
    
    // This is my like button action method.
    @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!) {
                 sender.setBackgroundImage(UIImage.init(named: "like (1).png"), forState: .Normal)
                 selectedindex.removeObject(selectindex!)
            }else{
                 sender.setBackgroundImage(UIImage.init(named: "like.png"), forState: .Normal)
                 selectedindex.addObject(selectindex!)
            }
    
    }
    
    0 讨论(0)
  • 2021-01-27 13:09

    If you're using the click on the entire cell, you can override the setSelected func in your custom cell just like that.

    override func setSelected(selected: Bool, animated: Bool) {
         super.setSelected(selected, animated: animated)
         if selected {
             self.checkBook.image = UIImage(named: "check")
         } else {
             self.checkBook.image = UIImage(named: "uncheck")
        }
    }
    
    0 讨论(0)
  • 2021-01-27 13:12

    Try to do it like this:

    var checkBook = UIImageView()
    
    if self.checkBook.image == UIImage(named: "check"){
       self.checkBook.image = UIImage(named: "uncheck")
    }
    else{
       self.checkBook.image = UIImage(named: "check")
    }
    
    0 讨论(0)
提交回复
热议问题