简体   繁体   English

将数据从自定义单元格(在ViewController的表中)传递到ViewController本身Swift3 iOS

[英]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分配datePickerindexPath

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.

相关问题 iOS Swift:将数据从ViewController传递到UITableViewCell - iOS Swift: Pass data from ViewController to UITableViewCell 如何在Swift中将值从自定义单元格设置/传递给ViewController - How to set/pass value from a custom cell to a viewcontroller in Swift 斯威夫特:如何传递子视图控制器prepareForSegue的数据以更改父视图控制器表视图单元格的TextLabel? - Swift: How can I pass data from child viewcontroller prepareForSegue to change the TextLabel of a cell of parent viewcontroller tableview? Swift / iOS:如何将数据从 AppDelegate 传递到 ViewController 标签? - Swift / iOS: How to pass data from AppDelegate to ViewController label? 从Swift文件(不是ViewController)将图像传递到ViewController - Pass Image to A ViewController from a Swift File (Which Is Not A ViewController) 将数据从AppDelegate实时传递到ViewController(iOS)Swift 2 - Pass data from AppDelegate to ViewController in realTime (iOS) Swift 2 如何将数据从 XIB ViewController 传递到 iOS Swift 中的 TabBarController - How to pass data from XIB ViewController to TabBarController in iOS Swift 如何在iOS Swift中将数据从ViewController传递给UITabBarController? - How to pass data from ViewController to UITabBarController in iOS Swift? 如何在iOS swift中将数据传递给以前的viewController? - how to pass data to previous viewController in iOS swift? Swift 4-将数据从ViewController传递到ViewController到标签栏 - Swift 4 - pass data from ViewController to a ViewController to a tab bar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM