重用单元格不能正常工作 - TableView

问题描述 投票:0回答:6

关于我的手机按钮我有问题。在我的tableView中,每一行由以下内容组成:图像,一些标签和一个按钮。该按钮具有复选标记图像。单击时,按钮的图像会发生变化。问题是另一个按钮的图像也无缘无故地改变了。发生这种错误是因为我的细胞被重复使用。

我曾尝试在TableViewCell中使用prepareForReuse方法,但没有任何反应。我也试过selectedRowAt,但我没有任何结果。请帮我。

图1:

Image 1

图2:

Image 2

这是我在custom Cell:的功能

  override func prepareForReuse() {
    if checkStr == "uncheck"{
        self.checkBook.setImage(uncheck, for: .normal)
    } else if checkStr == "check"{
        self.checkBook.setImage(check, for: .normal)
    }
}

func isPressed(){
    let uncheck = UIImage(named:"uncheck")
    let check = UIImage(named: "check")
    if self.checkBook.currentImage == uncheck{
        checkStr == "check"
    } else self.checkBook.currentImage == check{
        checkStr == "uncheck"
    }
}

在我的tableView

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let selectedCell: ListPropertyUserCell = tableView.cellForRow(at: indexPath) as! ListPropertyUserCell
    let uncheck = UIImage(named:"uncheck")
    let check = UIImage(named: "check")
    if selectedCell.checkBook.imageView?.image == uncheck{
        selectedCell.checkStr = "check"
    } else if selectedCell.checkBook.imageView?.image == check{
        selectedCell.checkStr = "uncheck"
    }
}
ios swift uitableview uibutton tableviewcell
6个回答
1
投票

根据帖子中的信息,这看起来像是一个单元重用问题。问题是tableView重用细胞而不是创建新细胞来维持性能。如果您尚未重置单元的状态,则重用的单元将保持配置为旧状态。

要快速修复,可以在prepareForReuse上实现UITableViewCell方法。

但是,如果要在滚动tableView后选中复选框,则需要在视图控制器中存储“已选中”的单元格。您可以自己存储,也可以使用tableView的didSelectRowAtIndexPath方法。


0
投票

尝试这样做:

var checkBook = UIImageView()

if self.checkBook.image == UIImage(named: "check"){
   self.checkBook.image = UIImage(named: "uncheck")
}
else{
   self.checkBook.image = UIImage(named: "check")
}

0
投票

如果您使用整个单元格上的单击,则可以像这样覆盖自定义单元格中的setSelected func。

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
投票

UITableViewCell是可重用的。您无法在单元格中存储视图状态。你应该设置单元格

func tableView(UITableView, cellForRowAt: IndexPath)  

数据源的方法

实现这一目标的最简单方法是实现

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

UITableViewDelegate的方法

然后,您可以在这些方法和cellForRowAtIndexPath设置单元格中的某些数组中添加/删除indexPath。

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
投票

试试我的代码。这里selectindex用于获取选定的单元格索引,selectedindex是NSMutableArray,我存储所有选定的单元格值。

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
投票

我最近遇到了这个问题,并没有找到太多关于它的问题。经过多次搜索后,解决的问题是使用:

override func prepareForReuse() {

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

只需将此方法放入自定义UITableViewCell中,并设置要重新分配统计信息的项目。

© www.soinside.com 2019 - 2024. All rights reserved.