Swift:分段控件在 UITableView 单元格中以一种奇怪的方式运行 [英] Swift: Segmented control behaves in a weird way in UITableView Cell

查看:23
本文介绍了Swift:分段控件在 UITableView 单元格中以一种奇怪的方式运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我点击 UICell 中的分段控件时,其他单元格会立即在同一位置获取此分段控件.看起来分段控件不仅可以识别这个特定的控件,还可以识别其他单元格中的其他控件.

Anytime I tap segmented control in UICell, immediately some other cell gets this segmented control in the same position. It looks like segmented control recognizes that not only this particular one was tapped but also some other one in other cell.

您遇到过这样的问题吗?

Have you ever encountered issue like this?

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

this is my custom cell implementation:

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 委托方法是值得的

Also, I think it is worth to provide my tableView delegate methods implemented

 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 行中的段控件也被移动了,尽管它应该处于默认位置.

To recap what the problem actually is: In every tableView cell there is a segment control. When I change the position of the one located in first row, I scroll down and see that segment control in row 5 also has been moved despite the fact it should be in the default position.

提前致谢

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

I recognized one of the biggest problem in solutions below - they are good as long as you don't use section in tableView. The thing is, from what I have discovered right now, in each sections the rows are counted over from 0.

推荐答案

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

This might be the cause when you are using reusing the cells, when you scroll the cell you changed will be shown again for another row.

为避免在重复使用单元格时出现这种情况,请确保同时重置其中的数据

To avoid this when you reuse cell make sure you reset the data in it also

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

In your case you have to check if the segmented value is changed then change the segmented control value also in cellForRowAtIndexPath

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

Please let me know if you need more explanation.

这里有一个示例项目sampleTableReuse

这篇关于Swift:分段控件在 UITableView 单元格中以一种奇怪的方式运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆