簡體   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