2016-10-01 77 views
0

我有一個待辦事項列表,我有一個標籤和一個按鈕在同一個單元格中,當我單擊按鈕時,更改該單元格的圖像按鈕,但是當我滾動表格視圖時,其他單元格上出現相同的按鈕,它沒有出現在單元格中,按鈕沒有被按下。爲什麼我的Tableview重用了一些單元格?

這裏是我的cellForRowAtIndexPath代碼

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! TaskTableViewCell 


    cell.label.text = "Task Number: \(indexPath.row + 1)" 
    cell.btnFavorite.setImage(UIImage(named: "star"), forState: .Normal) 
    cell.btnFavorite.setImage(UIImage(named: "star-filled"), forState: .Selected) 
    cell.btnFavorite.addTarget(self, action: #selector(ListOfTasksViewController.addRemoveFavoriteList), forControlEvents: .TouchUpInside) 


    return cell 
} 

func addRemoveFavoriteList(sender : UIButton) { 
    sender.selected = !sender.selected 

} 

定製TableViewCell類:

import UIKit 

class TaskTableViewCell: UITableViewCell { 

    @IBOutlet weak var label: UILabel! 
    @IBOutlet weak var btnFavorite: FavoriteButton! 

    override func awakeFromNib() { 
     super.awakeFromNib() 

    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

    } 

    override func prepareForReuse() { 
     super.prepareForReuse() 
     label.text = nil 
     btnFavorite.selected = false 

    } 

} 

視圖控制器:

import UIKit 

class ListOfTasksViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet weak var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.delegate = self 
     tableView.dataSource = self 

    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 20 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! TaskTableViewCell 


     cell.label.text = "Task Number: \(indexPath.row + 1)" 

     cell.btnFavorite.indexPath = indexPath.row 

     cell.btnFavorite.addTarget(self, action: #selector(ListOfTasksViewController.addRemoveFavoriteList), forControlEvents: .TouchUpInside) 


     return cell 
    } 

    func addRemoveFavoriteList(sender : FavoriteButton) { 

     if sender.selected { 
      sender.selected = false 


     } else { 
      sender.selected = true 

      let index = NSIndexPath(forRow: sender.indexPath, inSection: 0) 
      let cell = tableView.cellForRowAtIndexPath(index) as! TaskTableViewCell 


     } 

    } 




} 
+0

這不是一個 「問題」。重用是正確的和理想的行爲。畢竟,當你說'tableView.dequeueReusableCellWithIdentifier'時,你買了它;你在說什麼? – matt

+0

當您按下單元格上的按鈕時,您需要更新您的數據源。 –

回答

2

在表視圖中的細胞被再利用,從而你向下滾動,在返回sc之前,屏幕外的單元格將被放置在隊列的開始處重溫一個不同的indexPath。這可能會導致一些問題,因此您需要覆蓋自定義單元類中的prepareForReuse方法。

override func prepareForReuse() { 

    super.prepareForReuse() 

    label.text = nil 
    btnFavorite.selected = false 
} 
+0

我做到了這一點,但當我按下按鈕,我向下滾動按鈕改變不會出現在其他細胞,(直到有它的確定),但是當我再次滾動按鈕按不與相同的圖像,你知道爲什麼? –

+0

是的,因爲該單元格離開了屏幕,因此它已準備好重用,您需要確保您的dataSource反映通過單元格所做的任何更改。所以當你按下按鈕時,你必須更新一個反映dataSource數組中相應對象的按鈕狀態的值。 – Callam

+0

然後當單元格返回到屏幕上時,在您的cellForRowAtIndexPath中,您需要根據反映按鈕狀態的對象值設置按鈕的'selected'屬性。 – Callam

0

您需要在cellforaRowAtIndexPath中添加條件。

你需要在數組中添加標誌來跟蹤你的選擇。 然後在cellforRowAtIndexPath中檢查。

例如

button.selected= No 

if arrselectedIndexpath containObject:indexPath{ 
button.selected =yes 
} 
相關問題