[英]Moving data between custom cells in a dynamic table in Swift
Firstly, apologies but I'm new to all of this (iOS Dev & Swift). 首先,道歉,但我是所有新手(iOS Dev&Swift)。
I have a number of custom cells that I dynamically load into a tableview. 我有许多自定义单元格,可以动态加载到表格视图中。 One of my cells is a data picker that, when the date is changed by the user, I want to send the updated data to one of the other cells but I'm stumped.
我的一个单元格是一个数据选择器,当用户更改日期时,我想将更新的数据发送到其他单元格中的一个,但是我很困惑。 Help please.
请帮助。
Since your cells are dynamically loaded into the table, it is not possible to address a specific cell directly. 由于单元格是动态加载到表中的,因此无法直接寻址特定单元格。 You should trying changing the underlying data source when the user chooses a date, and call table.reloadData()
您应该在用户选择日期时尝试更改基础数据源,然后调用table.reloadData()
Assuming the cells are loaded and visible, you can pass a reference of one cell to another, but you'll need to create a couple of methods within your custom cells. 假设单元格已加载并且可见,您可以将一个单元格的引用传递给另一个单元格,但是您需要在自定义单元格中创建几个方法。
In my case I have two custom cells, a cell named CellBirthday that contains a label named birthDateLabel, and a cell that contains a DatePicker named CellDatePicker. 在我的情况下,我有两个自定义单元格,一个名为CellBirthday的单元格,其中包含一个名为birthDateLabel的标签,一个单元格,其名称为DatePicker,名为CellDatePicker。 I want to update birthDateLabel every time the DataPicker value changes.
我想每次DataPicker值更改时更新birthDateLabel。
I'll first load the cells and store a reference of CellBirthday inside CellDatePicker, then when the date picker changes, I'll update the label value inside CellBirthday. 我将首先加载单元格并将CellBirthday的引用存储在CellDatePicker中,然后当日期选择器更改时,我将在CellBirthday中更新标签值。 Here is the relevant code fragment to load the two cells.
这是加载两个单元的相关代码片段。 In this example, I use the same name for both the cell tag and class name, for example CellBirthday is both the cell tag and the class name specified in the storyboard:
在此示例中,我对单元格标记和类名称使用相同的名称,例如CellBirthday既是情节提要中指定的单元格标记和类名称:
let birthdayCell = tableView.dequeueReusableCell(withIdentifier: "CellBirthday") as! CellBirthday
let datePickerCell = tableView.dequeueReusableCell(withIdentifier: "CellDatePicker") as! CellDatePicker
datePickerCell.setBirthdayCell(BirthdayCell: birthdayCell)
And here are the custom classes: 这是自定义类:
class CellBirthday: UITableViewCell {
@IBOutlet fileprivate weak var birthDateLabel: UILabel!
var birthdayText: String? {
didSet {
birthDateLabel.text = birthdayText
}
}
}
class CellDatePicker: UITableViewCell {
@IBOutlet fileprivate weak var datePicker: UIDatePicker!
var birthdayCell: CellBirthday?
func setBirthdayCell(BirthdayCell birthdayCell: CellBirthday) {
self.birthdayCell = birthdayCell
}
func getDateString(FromPicker picker: UIDatePicker? = nil) -> String {
var dateText: String = ""
if picker != nil {
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate("MMMMdy")
dateText = dateFormatter.string(from: picker!.date)
}
return dateText
}
@IBAction func datePickerValueChange(_ sender: UIDatePicker) {
if birthdayCell != nil {
birthdayCell!.birthdayText = getDateString(FromPicker: sender)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.