如何修复 swift 中重复的 UITableCells

How to fix UITableCells repeating in swift

我知道这里已经有几个这样的问题,但我无法解决其中任何一个问题。基本上,我有一个 ui table 视图,每个单元格中都有一个标签和一个开关。每 14 个单元格开关将重复一次。 (开关 1 的状态将改变开关 14、28 等...)

这是我的代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Configure the cell:

    let cell = tableView.dequeueReusableCellWithIdentifier("CompanyCell", forIndexPath: indexPath) as! CompanyCell

    println(companies.count)
    cell.companyName.text = companies[indexPath.row]

    return cell
}

一个包含 31 个公司名称的数组被加载到单元格中:

[Apple Inc, American Express Co, ... , Exxon Mobil Corp, Dow Jones]

标签(公司名称)在单元格的左侧,开关在单元格的右侧。

那么我怎样才能让细胞停止相互重复使用呢?

这是因为您正在

重复使用单元格
tableView.dequeueReusableCellWithIdentifier("CompanyCell", forIndexPath: indexPath) as! CompanyCell 

为避免这种情况,请重置您的 cellForRowAtIndexPath 中的开关

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Configure the cell:

  let cell = tableView.dequeueReusableCellWithIdentifier("CompanyCell", forIndexPath: indexPath) as! CompanyCell
  cell.mySwitch.on = false // or Whatever your switch name is (from CompanyCell class)
  cell.companyName.text = companies[indexPath.row]

return cell
}

或像

这样创建新单元格
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Configure the cell:

  let cell = CompanyCell()
  cell.companyName.text = companies[indexPath.row]

return cell
}

当您使用此行重复使用单元格时

let cell = tableView.dequeueReusableCellWithIdentifier("CompanyCell", forIndexPath: indexPath) as! CompanyCell

屏幕外未使用的单元格将返回给您。这个未使用的单元格将具有其属性 - 根据其旧值设置的开关状态和公司名称。

现在之后,您正在根据您的数据更新要设置的companyName,这是完美的:

cell.companyName.text = companies[indexPath.row]

但是,这里您没有设置开关的状态,它仍将具有旧值。您将需要维护开关状态的数据,并在 cellForRowAtIndexPath 方法中相应地进行设置。像这样,其中 switchStateArray 是您的开关状态的数据源:

cell.yourSwitch.on = switchStateArray[indexPath.row]

现在您有一个代表这个包含 31 个公司名称的数组的模型。我个人倾向于一组自定义 Company 对象,这些对象不仅包含名称,还包含该公司的状态(即它的开关是打开还是关闭):

class Company {
    let name: String
    var state = false

    init(name: String) {
        self.name = name
    }
}

而且,我们还假设您已将单元格中的 UISwitch 连接到 CompanyCell class:

中的 IBOutlet
class CompanyCell : UITableViewCell {
    @IBOutlet weak var companyName: UILabel!
    @IBOutlet weak var companySwitch: UISwitch!
}

如果你有一个 属性,companies 是这些 Company 对象的数组,那么你就可以实现 UITableViewDataSource 方法:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return companies.count
}

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

    let company = companies[indexPath.row]
    cell.companyName.text = company.name
    cell.companySwitch.on = company.state

    return cell
}

并且您可能还想将单元格中开关的 "value changed" 操作连接到 table 视图控制器中的方法,如果开关状态发生变化,它会更新模型:

@IBAction func didChangeValueInSwitch(sender: UISwitch) {
    let cell = sender.superview?.superview as! CompanyCell
    let indexPath = tableView.indexPathForCell(cell)
    let company = companies[indexPath!.row]
    company.state = sender.on
}

在您的单元格中写 func prepareForReuse() class

class CompanyCell : UITableViewCell {
@IBOutlet weak var companyName: UILabel!
@IBOutlet weak var companySwitch: UISwitch!

override func awakeFromNib() {
    super.awakeFromNib()
 // Initialization code
}
override func prepareForReuse() {
//set your cell's state to default here
self.companySwitch.isOn = false
}
}

如果 UITableCells 图像视图重复,请使用函数 func prepareForReuse()

 class HomeFavoriteCell: UITableViewCell {

        @IBOutlet weak var imglLocation: UIImageView!

        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
        override func prepareForReuse() {
            //set your cell's state to default here

            self.imglLocation.image = nil
        }