滚动 tableview 时 UISegment 值发生变化 [英] UISegment value changing when tableview get scrolled

查看:36
本文介绍了滚动 tableview 时 UISegment 值发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UISegmentControl 在表格视图中显示目标类型的问题.但是,如果我在任何一个单元格中选择一个段,那么如果我滚动,一些段值就会改变.我不知道如何解决这个问题.请指导我.

I am using UISegmentControl to display objective type questions in table view. But, if I select one segment in any one of cell, then if I scroll, some segment values are changed. I dont know how to solve that. Kindly guide me.

单元格大小: 160px

段色调:蓝色

编码

//UIViewController
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = segTblVw.dequeueReusableCellWithIdentifier("segment", forIndexPath: indexPath) as! segmentTblCell

        return cell
    }

//UITableViewCell CLASS
class segmentTblCell: UITableViewCell {



    @IBOutlet weak var segMent: UISegmentedControl!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

屏幕截图如下:

推荐答案

方法一:通过将所有单元格对象保存在一个数组中来防止单元格的重用

Method 1: Prevent reusability of cells by, Holding all cell objects in an array

var arraysCells : NSMutableArray = []//globally declare this

viewDidLoad()

for num in yourQuestionArray//this loop is to create all cells at beginning 
{
     var nib:Array = NSBundle.mainBundle().loadNibNamed("SegmentTableViewCell", owner: self, options: nil)
     var cell = nib[0] as? SegmentTableViewCell
     arraysCells.addObject(cell!);
}

在 tableViewDelegate 中,

in tableViewDelegate,

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return arraysCells.objectAtIndex(indexPath.row) as! UITableViewCell
}

您可以通过迭代arraysCells

注意:如果您有大量单元格,方法 1 会很慢

NOTE: Method 1 will be slow, if you have big number of cells

方法 2: 像往常一样重用单元格,但使用委托和数组保存状态(输入的值).

Method 2: Reuse the cell as normal, but save the states(enterd values) Using Delegate and arrays.

在自定义 UITableViewCell

in custom UITableViewCell

@objc protocol SegmentTableViewCellDelegate {
func controller(controller: SegmentTableViewCell, selectedSegmentIndex:Int, indexPath : NSIndexPath)
}

class SegmentTableViewCell: UITableViewCell {

var delegate: AnyObject?
var indexPath : NSIndexPath?

@IBOutlet weak var segment: UISegmentedControl! //outlet of segmented Control

@IBAction func onSegmentValueChanged(sender: UISegmentedControl/*if the parameter type is AnyObject changed it as  UISegmentedControl*/)//action for Segment
{
    self.delegate?.controller(self, selectedSegmentIndex: sender.selectedSegmentIndex, indexPath: indexPath!)
}

在视图控制器中

class MasterViewController: SegmentTableViewCellDelegate{
var selectedAnswerIndex : NSMutableArray = []  //globally declare this
var selectedSegmentsIndexPath : NSMutableArray = [] //globally declare this


func controller(controller: SegmentTableViewCell, selectedSegmentIndex:Int, indexPath : NSIndexPath)
{
    if(selectedSegmentsIndexPath.containsObject(indexPath))
    {
        selectedAnswerIndex.removeObjectAtIndex(selectedSegmentsIndexPath.indexOfObject(indexPath))
        selectedSegmentsIndexPath.removeObject(indexPath)
    }
    selectedAnswerIndex.addObject(selectedSegmentIndex)
    selectedSegmentsIndexPath.addObject(indexPath)
}

在 cellForRowAtIndexPath (tableView Delegate)

in cellForRowAtIndexPath (tableView Delegate)

if(selectedSegmentsIndexPath.containsObject(indexPath))
{
 cell?.segment.selectedSegmentIndex = selectedAnswerIndex.objectAtIndex(selectedSegmentsIndexPath.indexOfObject(indexPath)) as! Int
}
cell?.delegate = self
cell?.indexPath = indexPath

你可以得到结果

for index in selectedSegmentsIndexPath
{
  var  cellIndexPath = index as! NSIndexPath
  var answer : Int = selectedAnswerIndex.objectAtIndex(selectedSegmentsIndexPath.indexOfObject(cellIndexPath)) as! Int
  NSLog("You have enterd answer \(answer) for question number \(cellIndexPath.row)")
}

这篇关于滚动 tableview 时 UISegment 值发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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