2016-10-01 32 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 
} 
相关问题