Swift3:为条件单元格从 main VC 调用函数数据到 TableVC

Swift3: Call func data from main VC to TableVC for conditional cells

我正在尝试将我的主视图控制器 (func getCityWeatherData) 中的一个功能扩展到下面的 class,该功能仅在特定条件下显示设置的项目。

我还标记了具有一些可供选择的标准的示例数据 - 我如何将它们与通常的数组分开?我已经在 numberOfRowsAt 部分尝试了我在其他地方读到的内容,但我很困惑。

class ProductRecsTableViewController: UITableViewController {

//Mark: Properties

var products = [Product]()

override func viewDidLoad() {
    super.viewDidLoad()

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if getCityWeatherData.humidity < 50 {
        return productsHumidity.count
    } else {
    return products.count
    } }

这是我创建的示例单元格 - 我想要 "weather: ..." 但要定义项目显示的时间。

guard let prod1 = Product(name: "XXX", photo: photo1, rating: 1, weather: "Humidity")

听起来你正试图从你的 ProductRecsTableViewController class 访问一个名为 getCityWeatherData 的函数,除非你在两者之间有某种类型的关系,否则你不能这样做控制器。您可以制作一个名为 ProductRecsTableViewControllerDelegate: AnyObject 的协议,它具有一个功能,即 returns 您的 ProductRecsTableViewController 您需要的信息。无论哪种方式,听起来您只需要通过委托或属性在两个控制器之间传递数据/信息。

更新:#2

class CustomViewController: UIViewController, CustomDelegate {
    func askForData() -> Data {
        return _whateverDataYouWant
    }
}

protocol CustomDelegate: AnyObject {
    func askForData() -> Data
}

class ProductRecsTableViewController: UITableViewController {
    weak var delegate: CustomDelegate?

    func myMethodThatNeedsAccessToData() {
        let data = delegate?.askForData()
        doStuffWith(data)
    }

}

在此过程中的某个时刻,您需要在 ViewController

中说 myProductsRectsTableViewController.delegate = self