Swift:在UITableView Cell中,分段控件的performance方式很奇怪

任何时候我在UICell中点击分段控制,立即其他一些单元在同一个位置获得这个分段控制。 它看起来像分段控制认识到,不仅这个特定的一个被窃听,而且在其他单元中的其他一个。

你有没有遇到这样的问题?

这是我的自定义单元实现:

class QuestionYesNoCustomCellTableViewCell: UITableViewCell { @IBOutlet weak var questionLabel: UILabel! @IBOutlet weak var segmentControl: ADVSegmentedControl! override func awakeFromNib() { super.awakeFromNib() // Initialization code segmentControl.items = ["TAK", "NIE"] segmentControl.font = UIFont(name: "Avenir-Black", size: 12) segmentControl.borderColor = UIColor.grayColor() segmentControl.selectedIndex = 1 segmentControl.selectedLabelColor = UIColor.whiteColor() segmentControl.unselectedLabelColor = UIColor.grayColor() segmentControl.thumbColor = UIColor(red: 46.0/255.0, green: 204.0/255.0, blue: 113.0/255.0, alpha: 1.0) segmentControl.addTarget(self, action: "segmentValueChanged:", forControlEvents: .ValueChanged) } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } func segmentValueChanged(sender: AnyObject?){ if segmentControl.selectedIndex == 0 { segmentControl.thumbColor = UIColor(red: 231.0/255.0, green: 76.0/255.0, blue: 60.0/255.0, alpha: 1.0) segmentControl.selectedLabelColor = UIColor.whiteColor() segmentControl.unselectedLabelColor = UIColor.grayColor() }else if segmentControl.selectedIndex == 1{ segmentControl.thumbColor = UIColor(red: 46.0/255.0, green: 204.0/255.0, blue: 113.0/255.0, alpha: 1.0) segmentControl.selectedLabelColor = UIColor.grayColor() segmentControl.unselectedLabelColor = UIColor.whiteColor() } } 

另外,我认为提供我的tableView委托方法是值得的

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return (dict2 as NSDictionary).objectForKey(dictKeysSorted[section])!.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell: QuestionYesNoCustomCellTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! QuestionYesNoCustomCellTableViewCell cell.questionLabel.text = (dict2 as NSDictionary).objectForKey(dictKeysSorted[indexPath.section])![indexPath.row] as? String if indexPath.row % 2 == 0 { cell.backgroundColor = UIColor(red: 245.0/255.0, green: 245.0/255.0, blue: 245.0/255.0, alpha: 1.0) } else { cell.backgroundColor = UIColor(red: 225.0/255.0, green: 225.0/255.0, blue: 225.0/255.0, alpha: 0.7) } return cell } func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return dictKeysSorted[section] } func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let headerCell = tableView.dequeueReusableCellWithIdentifier("CellHeader") as! CustomHeaderCell headerCell.backgroundColor = UIColor(red: 20.0/255.0, green: 159.0/255.0, blue: 198.0/255.0, alpha: 1.0) headerCell.headerLabel.text = dictKeysSorted[section] return headerCell } func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 70.0 } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return dictKeysSorted.count } func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 110.0 } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } 

回顾一下问题实际是什么:在每个tableView单元格中都有一个段控件。 当我改变位于第一行的那个位置时,向下滚动,看到第5行中的段控制也被移动,尽pipe它应该处于默认位置。

提前致谢

编辑:

我认识到以下解决scheme中最大的问题之一 – 只要你不使用tableView中的部分,他们是好的。 事情是,从我现在发现,在每个部分的行是从0开始计算的。

这可能是您使用重复使用单元格时的原因,当您更改的单元格滚动时将再次显示另一行。

为了避免这种情况,当你重用单元格确保你重置它的数据也

在你的情况下,你必须检查分段值是否改变,然后改变分段控制值cellForRowAtIndexPath

请让我知道,如果你需要更多的解释。

这里是sampleTableReuse的示例项目

这是因为UITableViewCells的可重用性。 您必须跟踪每个行的数据源选定段索引。 然后在cellForRowAtIndexPath中,您必须正确设置每个单元格。

定义一个枚举可能的地方回答:

 enum Answer { case Yes case No case None } 

然后定义并初始化你的答案数组:

 var answer = [Answer](count: numberOfQuestions, repeatedValue: .None) 

在你的单元的实现中添加一个方法来configuration一个单元格的答案

 func setupWithAnswer(answer: Answer) { var selectedIdex = UISegmentedControlNoSegment switch answer { case .Yes: selectedIdex = 0 case .No: selectedIdex = 1 default: break } self.segmentedControl.selectedSegmentIndex = selectedIdex } 

最后在你的cellForRowAtIndex出列之后做

 cell.setupWithAnswer(answer: self.answers[indexPath.row])