【问题标题】:Swift: Segmented control behaves in a weird way in UITableView CellSwift:分段控件在 UITableView Cell 中的行为方式很奇怪
【发布时间】:2015-10-27 23:02:02
【问题描述】:

只要我在 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 行中的段控件也已移动,尽管它应该位于默认位置。

提前致谢

编辑:

我认识到以下解决方案中最大的问题之一 - 只要您不使用 tableView 中的部分,它们就很好。问题是,根据我现在发现的情况,在每个部分中,行数都是从 0 开始计数的。

【问题讨论】:

    标签: ios swift uitableview uisegmentedcontrol


    【解决方案1】:

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

    为避免重复使用单元格时发生这种情况,请确保您也重置其中的数据

    在您的情况下,您必须检查分段值是否已更改,然后在 cellForRowAtIndexPath 中更改分段控制值

    如果您需要更多解释,请告诉我。

    这是给你的示例项目sampleTableReuse

    【讨论】:

    • 谢谢,如果您不介意,我会很高兴看到一些使其工作的代码。另外,我希望单元格保存分段控件的值,以便用户可以返回到它,这就是我害怕在 cellForRowAtIndexPath 中设置它的方式
    • 上述方法 cell.segmentControl.selectedIndex = 1 中的这一行也不起作用。同样,只要单元格超出屏幕,价值就会丢失
    • 我会给你一个示例项目,稍等片刻
    • 好吧,我正在深入研究:)会回复你!
    • 为什么你使用 _name 而不是 name 等?我删除了下划线进行测试,它不再起作用了,所以我想这很重要:)
    【解决方案2】:

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

    例子

    在某个地方定义一个带有可能答案的枚举:

    enum Answer {
        case Yes
        case No
        case None
    }
    

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

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

    在您的单元格的实现中添加一个方法来配置带有答案的单元格

    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])
    

    【讨论】:

    • 您能提供一些示例如何实现吗?
    • 我会检查并回复你结果:)
    • 还有一件事:一定要从你的单元格中设置一个委托给控制器以正确更新答案数组
    • 知道了,您能否详细说明一下这个委托,我应该在哪里设置它?
    • 您能否提供有关我的编辑的其余代码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多