UITableView cell with slider : touch not working correctly swift 2

守給你的承諾、 提交于 2019-12-23 12:36:46

问题


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

    }
}

回答1:


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
 }


来源:https://stackoverflow.com/questions/37316026/uitableview-cell-with-slider-touch-not-working-correctly-swift-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!