Search code examples
iosuitableviewswift2uislider

UISlider long touch to move it in a UITableView


I have a uitableview with multiples custom cells, the problem is that the UISlider in one of my customs cell, is slow I need to press it about half a second to be able to move it I supposed that I can use the delaysContentTouches to solve the problem but I m not sure if t's the solution, how to implement ? I supposed it's a problem of focus because I tried to do a simple project a uitableview with 20 sliders and it works perfectly. How can I solve the problem ?

Here is my code : For the UITableView cellForRowAtIndexPath method :

let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.SliderCell, forIndexPath: indexPath) as! SliderCell
    cell.delegate = self
    cell.slider.tag = indexPath.row
    cell.display(block)
    cell.selectionStyle = UITableViewCellSelectionStyle.None
    return cell

and here for the slider cell :

class SliderCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var maxLegendLabel: UILabel!
    @IBOutlet weak var minLegendLabel: UILabel!
    @IBOutlet weak var slider: UISlider!
    @IBOutlet weak var answerLabel: UILabel!

    var delegate: QuestionSliderCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
    }

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

    @IBAction func slideAction(sender: UISlider) {
        let sliderValue = Int(sender.value)
       print("slider value")
       print(sliderValue)
    }


    func display(block: Block){
        titleLabel.text = block.title
        slider.value = 1.0

    }
}

Solution

  • You should try to disable the content delay touches on your UITableView. This will make the slider more responsive and disable every delays on touch.

    To do so try this code below:

     override func viewDidLoad() {
            super.viewDidLoad()
            yourTableView.delaysContentTouches = false
     }
    
     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            for view in tableView.subviews {
                if view is UIScrollView {
                    (view as? UIScrollView)!.delaysContentTouches = false
                    break
                }
            }
            return numberOfRows
     }