0

I want to implement picker that is in my custom cell, in my VC that has the tableView, I can't figure out how to reference that specific picker (because I have other 2), so if I try to make a switch statement with "did select row" I can't reference that specific picker that is in tableCell. thank you

3
  • Can you make your custom cell the picker view delegate? Posting code and more detail might help answer this. Commented Sep 8, 2022 at 15:55
  • I could delegate in my custom cell, but can't find a way to pass the data to tableview and update it when did select row Commented Sep 8, 2022 at 16:49
  • @CristianTamborrell - In general, if you have interactive elements in a table view cell, that cell class should handle the interaction and inform the controller that something changed. At that point, the controller should update the data. It sounds like you are trying to have the controller ask the cell for information. However, it's not entirely clear what you are asking... try to provide more detail about your cell / table view / controller structure, and describe exactly what you're trying to do. Commented Sep 9, 2022 at 19:02

1 Answer 1

0

You can use UIPickerView inside your UITableViewCell class like this:

class ViewController: UIViewController {
    
    @IBOutlet weak var lbl: UILabel!
    @IBOutlet weak var tableView: UITableView!
    
    let list1:[String] = ["iOS","Swift","Xcode"]
    let list2:[String] = ["android","Kotlin","Android Studio"]
    let list3:[String] = ["React","Javascript","VSCode"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        // Do any additional setup after loading the view.
    }

}

//MARK: PICKERVIEW DELEGATE AND DATASOURCE
extension ViewController: UIPickerViewDataSource, UIPickerViewDelegate {
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch pickerView.tag {
        case 0:
            return list1.count
        case 1:
            return list2.count
        case 2:
            return list3.count
        default:
            return 0
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch pickerView.tag {
        case 0:
            return list1[row]
        case 1:
            return list2[row]
        case 2:
            return list3[row]
        default:
            return nil
        }
    }
    
    func pickerView(_ pickerView: UIPickerView,
                  didSelectRow row: Int,
                  inComponent component: Int) {
        switch pickerView.tag {
        case 0:
            lbl.text = list1[row]
        case 1:
            lbl.text = list2[row]
        case 2:
            lbl.text = list3[row]
        default:
            break
        }
        
    }
}

//MARK: - TABLEVIEW DELEGATE DATASOURCE
extension ViewController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomPickerTableViewCell", for: indexPath) as! CustomPickerTableViewCell
        cell.pickerview.delegate = self
        cell.pickerview.dataSource = self
        cell.pickerview.tag = indexPath.row
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
}

The tableview cell Class:

class CustomPickerTableViewCell: UITableViewCell {
    
    @IBOutlet weak var pickerview: UIPickerView!
    
}

Make the storyboard design look like this:

storyboard screenshot

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.