【问题标题】:custom view with parameter from a remote json带有来自远程 json 的参数的自定义视图
【发布时间】:2021-09-09 16:11:53
【问题描述】:

我有一个自定义视图和它的 VC。自定义视图中有各种各样的东西,其中之一是给定产品可用尺寸的标签。 :

class CustomView: UIView {

    var productSize: Array<String>
    // all other labels and stuff

    required init(size: Array<String>) {
        self.productSizes = size
        super.init(frame: .zero)
        setupView()
        setupConstraints()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

还有另一种观点:

class CustomViewViewController: UIViewController {

    private let productDetailView = ProductDetailsView(size: [])
    let product: Product

}

我得到了产品没有任何问题,但我应该如何将可用尺寸与视图联系起来?显然那个空数组不正确,我应该如何将远程文件中的数据与视图初始化连接起来? 我正在考虑制作一个数组并使用产品本身从原始表格视图中推送它,如下所示:

if let productsArray = self.productsArray {
    let allSizes: Array<String> = productsArray[indexPath.row].sizes?.components(separatedBy: ",") ?? []
    let customVC = CustomViewViewController(product: productsArray[indexPath.row], allSizes: allSizes)
    navigationController?.pushViewController(customVC, animated: true)
}

但我也无法真正弄清楚这个应该如何工作......

【问题讨论】:

    标签: swift view uiviewcontroller init


    【解决方案1】:

    您可以将其设为lazy var

    lazy var productDetailView:ProductDetailsView = { 
       let res = ProductDetailsView(size:self.product.sizes?.components(separatedBy: ",") ?? [])
       return res
    }()
    

    1-var productDetailView:ProductDetailsView!

    2- 内部viewDidLoad

    productDetailView = ProductDetailsView(size:self.product.sizes?.components(separatedBy: ",") ?? [])
    

    【讨论】:

      猜你喜欢
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多