【发布时间】:2025-12-08 11:10:02
【问题描述】:
早安,
我已经设法让我的电子表格视图工作,但没有从电子表格视图中检索用户输入数据的功能,只有一个填充单元格的功能,如下所示。有人可以帮我解决这个问题。电子表格视图中的第一列是使用填充到标签中的数组自动填充的,效果很好。然后第二列由 TextFields 组成,允许用户输入自己的数据,现在我想在按下下一个选项卡时将该数据保存到一个数组中并显示一个图表。我试图填充数组
func spreadsheetView(_ spreadsheetView: SpreadsheetView, cellForItemAt indexPath: IndexPath) -> Cell? {
if case (0, 0) = (indexPath.column, indexPath.row) {
let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: CellInfo.identifier, for: indexPath) as! CellInfo
cell.label.text = "STATION"
cell.backgroundColor = .systemGray
return cell
} else if case (1, 0) = (indexPath.column, indexPath.row) {
let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: CellInfo.identifier, for: indexPath) as! CellInfo
cell.Rlabel.text = "MAG READING"
cell.backgroundColor = .systemGray
return cell
} else if case (0, 1...(GlobalVariable.sharedInstance.numberOfStations)) = (indexPath.column, indexPath.row) {
let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: MyLabelCell.identifier, for: indexPath) as! MyLabelCell
cell.setup(with: GlobalVariable.sharedInstance.arrayProfileStations[indexPath.row-1])
//print(GlobalVariable.sharedInstance.arrayProfileStations[indexPath.row-1])
cell.backgroundColor = .systemGray6
return cell
} else if case (1, 1...(GlobalVariable.sharedInstance.numberOfStations)) = (indexPath.column, indexPath.row) {
let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: CellInfo.identifier, for: indexPath) as! CellInfo
var array = [String]()
cell.TextField.text = "-"
// if GlobalVariable.sharedInstance.MagDataEntries {
// cell.TextField.text = GlobalVariable.sharedInstance.MagDataEntries[indexPath.row-1]
// }else{
//Populate the array with user data
//
array.append(cell.TextField.text ?? "0")
//GlobalVariable.sharedInstance.MagDataEntries.append(contentsOf: array)
//print(indexPath.row-1)
print(array)
//}
return cell
// } else if case (1, 1...(GlobalVariable.sharedInstance.numberOfStations)) = (indexPath.column, indexPath.row) {
//
// let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: MyLabelCell.identifier, for: indexPath) as! MyLabelCell
// cell.setup(with: GlobalVariable.sharedInstance.arrayProfileStations[indexPath.row-1])
// spreadsheetView.
// return cell
}
return nil
}
以下是标签文本字段的创建方式:
class MyLabelCell:Cell{
static let identifier = "MyLabelCell"
public let label = UILabel()
public func setup(with text:String){
label.text = text
label.textAlignment = .center
//label.isUserInteractionEnabled = true
contentView.addSubview(label)
}
override func layoutSubviews() {
super.layoutSubviews()
label.frame = contentView.bounds
}
}
class CellInfo: Cell {
let label = UILabel()
let Rlabel = UILabel()
let TextField = UITextField()
static let identifier = "CellID"
override init(frame: CGRect) {
super.init(frame: frame)
label.frame = bounds
label.autoresizingMask = [.flexibleWidth, .flexibleHeight]
label.font = UIFont.boldSystemFont(ofSize: 14)
label.textAlignment = .center
//label.backgroundColor = .darkGray
contentView.addSubview(label)
Rlabel.frame = bounds
Rlabel.autoresizingMask = [.flexibleWidth, .flexibleHeight]
Rlabel.font = UIFont.boldSystemFont(ofSize: 14)
Rlabel.textAlignment = .center
//Rlabel.backgroundColor = .darkGray
contentView.addSubview(Rlabel)
TextField.frame = bounds
TextField.autoresizingMask = [.flexibleWidth, .flexibleHeight]
TextField.font = UIFont.boldSystemFont(ofSize: 16)
TextField.textAlignment = .center
contentView.addSubview(TextField)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
【问题讨论】: