Search code examples
iosswiftuipickerview

Using UIButton within the cell of UIPickerView


In my app I have a settings page, and I would like a UIPickerView that works as a check box(lists items and you can enable or disable them). I've looked up different solutions and all I could find was using viewForRow and using UIButton. I am able to add the buttons and it looks good but the buttons don't seem to be tappable.

Heres my implementation:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView{
    let button = UIButton(type: .system)
    button.addTarget(self, action: #selector(screenSelectorButtonPress(_:)), for: .touchUpInside)
    button.tag = 0
    if row == 0{
        button.setTitle(Array3[row], for: .normal)
    }else{
        button.setTitle("\u{2610} \(Array3[row])", for: .normal)
    }
    return button
}

@objc func screenSelectorButtonPress(_ sender:UIButton!){
    print("test")
}

The cells all populate properly but tapping them does nothing. The functionality that I want is exactly as seen here in safari.

Is there another way to do this or am I doing something wrong with the button? Also yes I could just use switches but our Android version of the app uses a check list like how safari does and we would like to keep it consistent.


Solution

  • You should implement

    func pickerView(_ pickerView: UIPickerView, 
            didSelectRow row: Int, 
             inComponent component: Int)
    

    //

    self.pickerView.delegate = self
    

    //

    class ViewController: UIViewController , UIPickerViewDelegate,UIPickerViewDataSource , UIGestureRecognizerDelegate {
    
        let pickerView = UIPickerView()
    
        override func viewDidLoad() {
    
            super.viewDidLoad()
    
            pickerView.frame = CGRect(x: 0, y: 30, width: self.view.frame.width, height: 300)
    
            pickerView.delegate = self
    
            pickerView.dataSource = self
    
            view.addSubview(pickerView)
    
            let tap = UITapGestureRecognizer(target: self, action: #selector(pickerTapped))
    
            tap.delegate = self
    
            pickerView.addGestureRecognizer(tap)
    
        }
        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 1
        }
    
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return Array3.count
        }
    
        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            print(row)
        }
    
    
        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    
            if row == 0 {
    
                return Array3[row]
            }
            else {
    
                return "\u{2610} \(Array3[row])"
            }
    
        }
    
    
        @objc func pickerTapped(tapRecognizer: UITapGestureRecognizer) {
            if tapRecognizer.state == .ended {
                let rowHeight = self.pickerView.rowSize(forComponent: 0).height
                let selectedRowFrame = self.pickerView.bounds.insetBy(dx: 0, dy: (self.pickerView.frame.height - rowHeight) / 2)
                let userTappedOnSelectedRow = selectedRowFrame.contains(tapRecognizer.location(in: self.pickerView))
                if userTappedOnSelectedRow {
                    let selectedRow = self.pickerView.selectedRow(inComponent: 0)
                    pickerView(self.pickerView, didSelectRow: selectedRow, inComponent: 0)
                }
            }
        }
    
    
        func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
            return true
        }
    
    
    }