[英]pass data from custom cell(in table which is on ViewController) to the viewcontroller itself Swift3 iOS
I have a ViewController
. 我有一个ViewController
。 The ViewController
has a TableView
in it. ViewController
有一个TableView
。 The TableView
has a few custom cells (say one is with the DatePicker
, second is TextField
, third is whatever it is - that's not the case) - cells are described in a TableViewCell
TableView
具有一些自定义单元格(例如,一个是与DatePicker
一起使用的,第二个是TextField
,第三个是它的大小写-并非如此)-在TableViewCell
中描述了单元格
So, I'm pressing the one with DatePicker
, set the date " 01.01.2016 " on cell 所以,我用DatePicker
按下一个,在单元格上设置日期“ 01.01.2016 ”
ISSUE: I need to insert " 01.01.2016 " into a dictionary which is in the ViewController 问题:我需要在ViewController中的字典中插入“ 01.01.2016 ”
would appreciate any advice thanks 将不胜感激任何建议,谢谢
Create internal
variables on your viewController: 在viewController上创建internal
变量:
class ViewController: UIViewController {
// Keeping an internal reference to your datePicker
internal var datePicker: UIDatePicker?
// Keeping an internal reference to the indexPath, where you have the datepicker
internal var datePickerIndexPath: IndexPath?
// Your dictionary, where you want to save the date
internal var dictionary: [String: Any] = [:]
Assign the datePicker
and the indexPath
in cellForRowAt
, when you are dequeuing that cell 在将单元出队时,在cellForRowAt
分配datePicker
和indexPath
。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Dequeuing cells and some other logic
// Assign the cell's datePicker to the internal datePicker
self.datePicker = cell.datePicker
self.datePickerIndexPath = indexPath
}
And in the didSelectRowAt
function, just use the internal variables and your dictionary 在didSelectRowAt
函数中,只需使用内部变量和字典
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let datePickerIndexPath = self.datePickerIndexPath, datePickerIndexPath == indexPath {
dictionary["date"] = self.datePicker?.date
}
}
Hope this helps! 希望这可以帮助!
You could have a delegation method in the ViewController that the custom TableViewCells call when the date selection happens. 您可以在ViewController中有一个委托方法,当选择日期时,自定义TableViewCells会调用该方法。 The ViewController can assign itself as the cell's delegate in cellForRowAtIndexPath
. ViewController可以将自己分配为cellForRowAtIndexPath
单元的委托。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.