Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView cell with slider : touch not working correctly swift 2

I m using a UITableView to display multiple custom cells, and I have one with a UiSlider, the problem is that to move the slider I need to do a long press touch to be able to move it otherwise it don't move. I tried to do a simple project with multiples slider in a UITableView and it works perfectly. So I suppose I need to configure some thing around the touch but I don't know what. Here is my code :

I have this code in my view did load for gesture :

 override func viewDidLoad() {
        super.viewDidLoad()
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(FormViewController.dismissKeyboard))
        view.addGestureRecognizer(tap)

        // without this line bellow you can't select any cell to display multiple or single choice questions
        tap.cancelsTouchesInView = false
        if  self.navigationController!.respondsToSelector(Selector("interactivePopGestureRecognizer")) {
            self.navigationController!.view.removeGestureRecognizer(navigationController!.interactivePopGestureRecognizer!)
        }

        notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillHideNotification, object: nil)
        notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillChangeFrameNotification, object: nil)

    }
func dismissKeyboard() {
        view.endEditing(true)
    }

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 is the code for my slider cell :

import UIKit

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

    }
}
like image 891
fandro Avatar asked May 19 '16 06:05

fandro


1 Answers

When you use other components like sliders in UITableView and you want to get better responsiveness, you could try to disable every delay on your UITableview.

The code below work well for me:

 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
 }
like image 157
Aaleks Avatar answered Nov 15 '22 10:11

Aaleks