繁体   English   中英

如何修复在swift中重复的UITableCells

[英]How to fix UITableCells repeating in swift

我知道这里已经有一些这样的问题,但是我无法让它们中的任何一个起作用。 基本上,我有一个ui表视图,每个单元格中都有一个标签和一个开关。 交换机每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]

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

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

使用此行重用单元格时

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类中的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
}

而且你可能想要将单元格中的开关的“值已更改”操作连接到表视图控制器中的方法,如果开关状态发生更改,则会更新模型:

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

在UITableCells imageview重复的情况下,使用函数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
        }

在你的单元类中编写func prepareForReuse()

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

这是因为你正在重复使用单元格

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM