2016-08-01 19 views
1

我有一个xib视图,其中我带了一个tableView customcell xib。在这个自定义单元格中,我有一个复选框按钮,其行为类似于使用自定义单元格进行检查和取消选中。但是,当我点击第一个单元格复选框作为第9个单元格的第9行单元格,第18行单元格的倍数时,......也变成了勾号。同时滚动复选框刻度选项在单元格之间变化。我无法知道这是为什么发生.. ??为什么我在自定义单元格中的复选框在swift中选择和滚动时显示不同的行为?

我已经注册了电池厦门国际银行视图:

override func viewDidLoad() { 
    super.viewDidLoad() 
    //Register custom cell 
    let nib = UINib(nibName: "CustomOneCell", bundle: nil) 
    AddOnTableView.registerNib(nib, forCellReuseIdentifier: "addoncell") 
    } 
func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return ADDONITEMS.count 
} 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell:CustomOneCell = AddOnTableView.dequeueReusableCellWithIdentifier("addoncell") as! CustomOneCell 
    let item: AddOnItems = ADDONITEMS[indexPath.row] 
      cell.addOnName.text = item.name 
      cell.addOnPrice.text = "£\(item.price!)" 


    return cell 
} 

对于复选框,我加入了如下自定义类:

var isCheckedAddOnGlobal = Bool() 
class AddOnCheckBox: UIButton { 


let checkedImage = UIImage(named: "checkboxredtick.png")! as UIImage 
let unCheckedImage = UIImage(named:"checkbox untick.png")!as UIImage 

//bool property 
var ischecked:Bool = false{ 
    didSet{ 
     //print(ischecked) 
     if ischecked == true{ 
      self.setImage(checkedImage, forState: .Normal) 


     }else{ 
      self.setImage(unCheckedImage, forState: .Normal) 
     } 
    } 
} 

override func awakeFromNib() { 
    self.addTarget(self, action:#selector(CheckBox.buttonClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside) 
    self.ischecked = false 
} 

func buttonClicked(sender: UIButton) { 

    if (sender == self) { 
     if ischecked == true{ 
      ischecked = false 
      isCheckedAddOnGlobal = false 

     }else{ 
      ischecked = true 
      isCheckedAddOnGlobal = true 

     } 

    } 

    } 
} 
+0

你为什么不使用didSelectCell方法,而不是添加怪异的目标,只是用buttonClicked在didSelectCell方法,这将有助于 –

+0

我的按钮是在自定义单元格,所以我认为我没有添加目标我怎么能设置任何操作按钮单击,如果单元格内容在另一个超文本类型的UITableViewCell @ Lu_ –

+0

swift文件中检查我的答案,不要使用单元格作为按钮,它有自己的方法单击和其他动作 –

回答

2

发生这种情况,因为你正在重用的TableViewCell,为您解决你可以尝试这样的问题,首先创建一个Int数组,给你选定的行并使用cellForRowAtIndexPath方法中的数组。

var selectedItems = [Int]() 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell:CustomOneCell = AddO nTableView.dequeueReusableCellWithIdentifier("addoncell") as! CustomOneCell 
    let item: AddOnItems = ADDONITEMS[indexPath.row] 
    cell.addOnName.text = item.name 
    cell.addOnPrice.text = "£\(item.price!)" 
    cell.checkBoxBtn.tag = indexPath.row 
    if (selectedItems.contains(indexPath.row)) { 
     cell.checkBoxBtn.setImage(UIImage(named:"checkbox untick.png"), forState: .Normal) 
    } 
    else { 
     cell.checkBoxBtn.setImage(UIImage(named: "checkboxredtick.png"), forState: .Normal) 
    } 
    cell.checkBoxBtn.addTarget(self, action:#selector(self.buttonClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside) 
    return cell 
} 

func buttonClicked(sender: UIButton) { 
    if (self.selectedItems.contains(sender.tag)) { 
      let index = self.selectedItems.indexOf(sender.tag) 
      self.selectedItems.removeAtIndex(index) 
    } 
    else { 
      self.selectedItems.append(sender.tag) 
    } 
    self.tableView.reloadData() 
} 
+0

你已经在同一地点本身添加了所有的按钮动作。我想使用自定义类的按钮..我已经在我的代码中使用。 –

+0

你不能通过使用旧的方法来做到这一点,因为当你重复使用它时,它将不适用于'UITableViewCell'。 –

+0

代码工作正确..但我需要基于选择复选框来实现这么多的东西。所以你能让我明白buttonClicked –

0

最好的办法是在选择电池通话

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomOneCell 
    cell.buttonClicked() 
} 

,改变buttonClicked方法

func buttonClicked() { 
... 
} 
0

我会做一个对象,其中包含产品信息和一个布尔值,以检查如果产品已被选中或没有。

如果你这样做,复选标记将显示正确。在桌面视图上滚动时,每次显示新的单元格时都会加载数据。

现在,它只知道索引等9被选中,并且当您向下滚动并加载新单元格时,索引9将被再次自动选择。

尝试是这样的:

class Product { 
var productName = "Test" 
var isSelected: Bool = false 
} 

在你的cellForRowAtIndexPath

if product.isSelected == true { 
    cell.checkBoxBtn.setImage(UIImage(named:"checkbox untick.png"), forState: .Normal) 
} else { 
    cell.checkBoxBtn.setImage(UIImage(named: "checkboxredtick.png"), forState: .Normal) 
} 
+0

发送一些样品plz –

+0

上传你的项目到github,它更容易帮助,然后 – Grumme

+0

我已经粘贴了一个小例子。希望它能帮助你 – Grumme

相关问题