Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableViewCell内的UIStepper在点击时出现问题_Ios_Swift_Uitableview - Fatal编程技术网

Ios UITableViewCell内的UIStepper在点击时出现问题

Ios UITableViewCell内的UIStepper在点击时出现问题,ios,swift,uitableview,Ios,Swift,Uitableview,我需要在UITableView的一行中显示UIStepper(仅第二行-请参见下图) 因此,我实现了func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableViewCell,如下所示: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

我需要在UITableView的一行中显示UIStepper(仅第二行-请参见下图)

因此,我实现了
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableViewCell
,如下所示:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCellWithIdentifier("OptionCell")!
  let debugModeOptionType = DebugModeOptionsType(rawValue: indexPath.row)

  switch(debugModeOptionType!) {
  case .DummyCurrentLocation:
    cell.textLabel!.text = "Dummy current location"
  case .StepLength:
    cell.textLabel!.text = "Step Length: \(stepLength)"

    // create a UIStepper
    let stepper = UIStepper(frame: CGRectMake(220, 10, 100, 10))

    // customize UIStepper
    stepper.autorepeat = true
    stepper.value = stepLength
    stepper.minimumValue = 0.1
    stepper.stepValue = 0.02
    stepper.maximumValue = 1.5

    stepper.addTarget(self, action: #selector(adjustStepLength(_:)), forControlEvents: UIControlEvents.AllEvents)

    // add UIStepper into the cell
    stepper.translatesAutoresizingMaskIntoConstraints = false
    cell.contentView.addSubview(stepper)

    case .TrueHeading:
      cell.textLabel?.text = "True Heading: \(trueHeading)"
    case .MagneticHeading:
      cell.textLabel?.text = "Magnetic Heading: \(magneticHeading)"
    case .HeadingAccuracy:
      cell.textLabel?.text = "Heading Accuracy: \(headingAccuracy)"
    case .CurrentDirection:
      cell.textLabel?.text = "Current Direction: \(currentDirection)"
    case .DrawWalking:
      cell.textLabel?.text = "Draw walking while navigating"
    }

    if selectedDebugModeOptions.contains(debugModeOptionType!) {
      cell.accessoryType = UITableViewCellAccessoryType.Checkmark
    } else {
      cell.accessoryType = UITableViewCellAccessoryType.None
    }

    return cell
}
但是,当我在真实设备上触摸UIStepper时(在模拟器内不会发生这种情况),会发生以下情况:


当这种情况发生时,其他单元格的UIStepper也开始闪烁。为什么会出现这样的问题?

我无法解释为什么只有在真实设备上才会出现这种情况,但由于表视图单元格是重复使用的,因此在以编程方式向单元格添加元素时必须小心,因为这些元素(如步进器)将在重复使用单元格时传播到其他单元格

有(至少)两种方法可以解决这个问题:

  • 在可重用单元出列后检查是否存在步进器,如果它位于不需要步进器的行上,则将其移除。您可以给步进器一个唯一的标记号(例如
    123
    ),然后搜索带有该标记的子视图并将其删除

    let stepperTagNumber = 123
    let cell = tableView.dequeueReusableCellWithIdentifier("OptionCell")!
    let debugModeOptionType = DebugModeOptionsType(rawValue: indexPath.row)
    
    if let stepper = cell.contentView.viewWithTag(stepperTagNumber) {
        // We have a stepper but don't need it, so remove it.
        if debugModeOptionType != .StepLength {
            stepper.removeFromSuperview()
        }
    } else {
        // We don't have a stepper, but need one.
        if debugModeOptionType == .StepLength {
            // create a UIStepper
            let stepper = UIStepper(frame: CGRectMake(220, 10, 100, 10))
            stepper.tag = stepperTagNumber  // This is key, don't forget to set the tag
    
            // customize UIStepper
            stepper.autorepeat = true
            stepper.value = stepLength
            stepper.minimumValue = 0.1
            stepper.stepValue = 0.02
            stepper.maximumValue = 1.5
    
            stepper.addTarget(self, action: #selector(adjustStepLength(_:)), forControlEvents: UIControlEvents.AllEvents)
    
            // add UIStepper into the cell
            stepper.translatesAutoresizingMaskIntoConstraints = false
            cell.contentView.addSubview(stepper)
        }
    }
    
    或:

  • 为tableview创建第二个原型单元(称为“OptionCellWithStepper”)。将步进器添加到故事板中的该单元。然后,当您调用带有标识符的可重用单元时,对case
    使用
    “OptionCellWithStepper”
    ,对所有其他情况使用identifier
    “OptionCell”
    。这样做,您不必以编程方式添加步进器,也不必记住为其他单元格删除步进器

    let debugModeOptionType = DebugModeOptionsType(rawValue: indexPath.row)
    let cellID = (debugModeOptionType == .StepLength) ? "OptionCellWithStepper" : "OptionCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellID)!