gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:10:10 26 4
gpt4 key购买 nike

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()
}
}

}

我的 Controller

// 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
}
}
}

最佳答案

您只需在 pickerView 委托(delegate)方法中设置表格单元格的 self.detailLabel.text

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

您需要修改您的cellForRowAt indexPath: TableView 数据源方法以返回cell.detailLabel.text 的值,这意味着您的最终数据源—— dataArray 中的 itemData 字典——需要有一个新条目,持续时间由选择器设置。

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

关于uitableview - UITableView 内部的 UIPickerView 值(基于 DateCell)更改 detailLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42526437/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com