代码之家  ›  专栏  ›  技术社区  ›  user8894520

显示pickerview中的数据

  •  -1
  • user8894520  · 技术社区  · 7 年前

    我为pickerview设计了一个带有文本字段的tableview单元格。我在tableview中加载了其中的两个单元格。为了显示pickerview中的数据,我在 cellForRowAt...

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell: sellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "sellProductIdentifier") as! sellTableViewCell
            cell.delegate = self
    
            let pickerView = UIPickerView()
            pickerView.delegate = self
            cell.pickerField.inputView = pickerView
            cell.pickerField.text = myData[0]//myData is an array with values from 1-10
    
            myCell = cell //The issue seems to be here.
    
            return cell
        }
    

    我给出的选取器视图方法如下。。。

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return myData.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return myData[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    
        myCell.pickerField.text = myData[row]
    }
    

    编辑: cellForRowAt ...

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    
    
        let cell: sellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "sellProductIdentifier") as! sellTableViewCell
        cell.delegate = self
    
        let pickerView = MyPickerView()
        pickerView.cell = cell
        pickerView.delegate = self
        cell.qtyPickerField.inputView = pickerView
    
        return cell
    }
    

    这是选取器视图方法。。。

            func numberOfComponents(in pickerView: UIPickerView) -> Int {
                return 1
            }
    
            func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
                return myData.count
            }
    
            func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
                return myData[row]
            }
    
            func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    
    
                if pickerView is MyPickerView {
    
                    if let cell = (pickerView as! MyPickerView).cell {
                        let indexpath = self.tableview.indexPath(for: cell)
                        if let index = indexpath {
                            (pickerView as! MyPickerView).cell?.qtyPickerField?.text = myData[index.row]
    
        }
        }
        }
    
    }
    

    编辑2 . pickerview代码 didSelectRow

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    
    if pickerView is MyPickerView {
    
            if let cell = (pickerView as! MyPickerView).cell {
    
                cell.qtyPickerField.text = myData[row]
                let indexPath = self.tableview.indexPath(for: cell)
                if let index = indexPath {
                    (pickerView as! MyPickerView).cell?.qtyPickerField?.text = myData[index.row]
                }
            }
    
        }
    }
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   Hussaan S    7 年前

    问题始于

    myCell = cell;
    

    在这里,您将始终拥有最新单元格的实例。所以在委托中赋值将更新最新调用中的textField。

    在pickerView delegate中,您可以做的是(不过没有其他方式:p)

    if let cell = tableView.cellForRow(at: INDEXPATH_OF_RESPECTIVE_ROW) { 
        "get text field instance and assign value."
    }
    
        2
  •  0
  •   Sandeep Bhandari    7 年前

    问题:

    正如上面回答中提到的那样

    myCell = cell //问题似乎就在这里。

    是罪魁祸首。因为每次 cellForRowAtIndexPath 完成后,myCell将引用最后返回的单元格,而不是负责显示 pickerView .

    第一次删除 迈塞尔=细胞 从…起 cellForRowAtIndexPath单元

    解决方案:

    现在,您需要做的就是找出哪个单元格包含负责显示pickerView的textField。

    现在可以有多种解决方案。这里有一个

    第1步: 创建PickerView的子类

    class MyPickerView : UIPickerView {
        weak var cell : MyTableViewCell? = nil
    }
    

    在代码中使用此pickerView比 UIPickerView .

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell: sellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "sellProductIdentifier") as! sellTableViewCell
            cell.delegate = self
    
            let pickerView = MyPickerView()
            pickerView.cell = cell //pass cells reference 
            pickerView.delegate = self
            cell.pickerField.inputView = pickerView
            //other code
    }
    

    最后

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            if pickerView is MyPickerView {
                if let cell = (pickerView as! MyPickerView).cell {
                    cell.textField?.text = myData[row]
                    let indexPath = self.tableView.indexPath(for: cell)
                    if let index  = indexPath {
                        //now go ahead and update your data
                    }
    
                }
    
            }
        }
    

    编辑:

    此解决方案可以有多种变体。有些人会同意,将单元格引用传递给选取器视图是可以的,因为无论如何,单元格都会持有对选取器的强引用,而选取器只持有对单元格的弱引用。

    我个人觉得将indexPath作为参数传递给picker更有意义。最后,当用户选择某个内容时,您将使用传递给选择器的indexPath获取对单元格的引用,并最终更新单元格的textField。

    订单号:

    enter image description here

    编辑2:

    由于OP仍然感到困惑,我决定修改他的编辑2,并将其作为答案的一部分

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    
            if pickerView is MyPickerView {
    
                if let cell = (pickerView as! MyPickerView).cell {
    
                    cell.qtyPickerField.text = myData[row]
                    let indexPath = self.tableview.indexPath(for: cell)
                    if let index = indexPath {
                        //this is only to show you how you can access indexPath
                        //and in future if you need to update your data source you can use it
                        //for now ignore this :)
                    }
                }
    
            }
        }
    

    应该足够好:)希望有帮助:)

        3
  •  0
  •   Dattatray Deokar    7 年前

    您需要在自定义单元格类sellTableViewCell中实现选择器视图委托

    您应该在自定义单元格中实现选择器委托,这样就不必引用单元格并更新该单元格textfield中的文本。如果在单元格中实现了这些委托方法,则必须更新该单元格实例的textfield中的文本。因此,不同单元格中不会混合文本。同时,您必须在数据模型对象中为数组中的indexPath更新该值,以便在滚动时保留该值。在上查看我的答案 this 链接以了解如何实施。