Делегат UIPickerView внутри TableViewCell не вызывается - PullRequest
0 голосов
/ 04 августа 2020

У меня есть TableViewCell, содержащий UIPickerView. Проблема в том, что делегат UIPickerView не вызывается

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "testCell") as! TableViewCell
        
        cell.setupCell(indexPath: indexPath.row)
        
        return UITableViewCell()
    }
}

class TableViewCell: UITableViewCell {

    public var picker = UIPickerView()
    private var index = 1
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        print("awakeFromNib")

        picker.dataSource = self
        picker.delegate = self        
    }
    
    public func setupCell(indexPath: Int) {
        print("setupcell")
        index = indexPath

        contentView.addSubview(picker)
        
        picker.translatesAutoresizingMaskIntoConstraints = false
        picker.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor).isActive = true
        picker.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor).isActive = true
        picker.topAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
        
    }
}

extension TableViewCell: UIPickerViewDataSource, UIPickerViewDelegate {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {        
        return 3
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 10
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return "abc"
    }
}

Я пытался перезагрузить сборщик, используя picker.reloadAllComponents(), но затем вызывается только func numberOfComponents(in pickerView: UIPickerView). Что мне не хватает? Проблема точно с моим мобильным.

1 Ответ

1 голос
/ 04 августа 2020

Вам нужно вернуть ячейку, которую вы установили вместо новой ячейки UITableViewCell()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "testCell") as! TableViewCell
        
        cell.setupCell(indexPath: indexPath.row)
        
        return cell //UITableViewCell()
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...