UITableView内的UIPickerView值(基于DateCell)更改detailLabel

时间:2017-03-01 08:14:10

标签: uitableview swift3 uipickerview ios10

enter image description here

我尝试将我的PickerView值显示在我的UITableView详细信息标签上。所以我设置了FormCell.swift

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  self.detailLabel.text = "\(component)\(row)"
}

然而,当我点击下一个单元格时,我的detailLabel错过了之前的值。(我的默认标签是" 01min 30sec")

enter image description here

有关此问题的任何想法?这是我关于这个问题的代码。

// FormCell.swift
import UIKit

class FormCell: UITableViewCell, UIPickerViewDelegate, UIPickerViewDataSource {

    // outlet
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var detailLabel: UILabel!
    @IBOutlet weak var pickerView: UIPickerView!

    // variable
    var isObserving = false
    let minutes = Array(0...59)

    // initialize pickerView delegate, dataSource
    override func awakeFromNib() {
        super.awakeFromNib()
        self.pickerView.delegate = self
        self.pickerView.dataSource = self

    }

    // tableViewCell's height setup
    class var expandedHeight: CGFloat { get { return 200 } }
    class var defaultheight: CGFloat { get { return 44 } }

    func checkHeight() {
        pickerView.isHidden = (frame.height < FormCell.expandedHeight)
    }


    // for pickerView
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return minutes.count
    }
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

        let pickerLabel = UILabel()
        var titleData = ""
        titleData = "\(minutes[row])"
        pickerLabel.text = titleData
        return pickerLabel
    }
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return pickerView.frame.width / 3
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.detailLabel.text = "\(component)\(row)"
    }




    // set observer for expanding
    func watchFrameChanges() {
        if !isObserving {
            addObserver(self, forKeyPath: "frame", options: [NSKeyValueObservingOptions.new, NSKeyValueObservingOptions.initial], context: nil)
            isObserving = true;
        }
    }
    func ignoreFrameChanges() {
        if isObserving {
            removeObserver(self, forKeyPath: "frame")
            isObserving = false;
        }
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "frame" {
            checkHeight()
        }
    }

}

我的控制器

// AddCircuitVC.swift
import UIKit

class AddCircuitVC: UITableViewController {

    let formCellID = "formCell"
    var selectedIndexPath: IndexPath?


    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 9
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let itemData = dataArray[indexPath.row]
        var cellID = wordCellID

            cellID = formCellID
            let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as? FormCell
            cell?.titleLabel.text = itemData[titleKey]

            return cell!


    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let previousIndexPath = selectedIndexPath

        if indexPath == selectedIndexPath {
            selectedIndexPath = nil
        } else {
            selectedIndexPath = indexPath
        }

        var indexPaths: Array<IndexPath> = []
        if let previous = previousIndexPath {
            indexPaths += [previous]
        }
        if let current = selectedIndexPath {
            indexPaths += [current]
        }

        if indexPaths.count > 0 {
            tableView.reloadRows(at: indexPaths, with: UITableViewRowAnimation.automatic)
        }

    }

    // observer
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
            (cell as! FormCell).watchFrameChanges()
    }
    override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
            (cell as! FormCell).ignoreFrameChanges()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        for cell in tableView.visibleCells {
            if cell.isKind(of: FormCell.self) {
                (cell  as! FormCell).ignoreFrameChanges()
            }
        }
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            if indexPath == selectedIndexPath {
                return FormCell.expandedHeight
            } else {
                return FormCell.defaultheight
            }
    }
}

2 个答案:

答案 0 :(得分:2)

您只是在self.detailLabel.text委托方法中设置了表格单元格的pickerView

因此,当您将屏幕上的单元格滚动然后再次在屏幕上滚动时,表格视图单元格会“忘记”之前设置的内容。

您需要修改cellForRowAt indexPath:表格视图数据源方法,以返回cell.detailLabel.text的值,这意味着您的最终数据源 - {{1}中的itemData字典 - 需要为选择器设置的持续时间有一个新条目。

我建议在dataArray中创建customTableViewCell时将itemData字典作为属性或参数传递,当用户选择持续时间时,设置字典条目并确保它获得已在cellForRowAt indexPath:保存/更新。

答案 1 :(得分:0)

首先,我创建全局变量sap.ushells.components.container.ApplicationContainer

timeSetUp

其次,我从我的控制器

编辑了import UIKit var timeSetup: String! class AddCircuitVC: UITableViewController {
cellForRowAt

最后,我在pickerView override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let itemData = dataArray[indexPath.row] var cellID = formCellID let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as? FormCell cell?.titleLabel.text = itemData[titleKey] // indexPath Check if indexPath != selectedIndexPath { if let timeSetup = timeSetup { cell?.detailLabel.text = timeSetup print("\(timeSetup)") } } else { print("from else \(timeSetup)") // initialize value to default one. cell?.pickerView.selectRow(1, inComponent: 0, animated: true) cell?.pickerView.selectRow(30, inComponent: 1, animated: true) cell?.detailLabel.text = "01min 30sec" } return cell! }

FormCell视图中获取detailLabel.text的数据
didSelectRow