繁体   English   中英

如何实现一个包含另外 2 个单元格的表格视图单元格,计数不一样

[英]How to implement a tableview cell that contains 2 more cells inside counts are not same

我正在尝试实现这样的布局在此处输入图像描述

  1. 我的表格视图有 3 个单元格(SubItemsCell(主单元格)、SubItemsListCell(主单元格内的产品数量)和 noSubstituteCell(第二个单元格计数 1 后的固定单元格))

  2. SubItemsCell 有一个“SELECT”按钮,它将展开并显示 SubItemsListCell,此单元格将作为(动态)主 SubItems 单元格下的任何产品计数加载。

  3. NoSubstitute Cell 出现在 n 个产品加载之后。

我的预期结果是首先加载主单元格,即 self.archivedProducts,当您单击每个归档产品的 select 按钮时,它会展开并加载 self.newproducts 并且没有替代 static 单元格。 因此,主单元格与这两个单元格配对,并且仅在我们扩展时立即显示。

到目前为止,我只加载了 Main Cell。 我不知道下一步该怎么做,请用代码或非常相似的例子给我一个想法。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 

  guard let cell = tableView.dequeueReusableCell(withIdentifier: "SubItemsCell", for: indexPath) as? SubItemsCell else {
        fatalError ("SubstituteItems Cell not found!")
   }
    
    let product = self.archivedProducts[indexPath.row]
    cell.titleLabel.text = product.title ?? ""
    cell.priceLabel.text = "AED \(String(format: "%.2f", product.price ?? 0.00))"
    cell.scaleLabel.text = product.uom ?? ""
    cell.unavailableLabel.isHidden = false
    cell.selectBtn.isHidden = false
    let imageUrl = product.image?.url
    let escapedUrl = imageUrl?.replacingOccurrences(of: "\\", with: "")
    let replacedUrl = "\(escapedUrl ?? "")"
    let url = URL(string: replacedUrl)
    let plImage = UIImage(named: "whiteBg")
    DispatchQueue.main.async {
    cell.thumbImage.kf.setImage(with: url, placeholder: plImage, options: [.transition(.fade(0.2))])
    }

}

我认为您不需要嵌套的表格视图。 您可以通过其他元素实现上述目标。 为了实现上面的布局,我的建议如下:

  1. 将您的 UITableViewCell 子类化为至少 2 种不同的格式。 有很多方法可以做到这一点,我的建议是使用带有 XIB 的 CocoaTouch Class ( 教程在这里),这样你就可以对布局和约束一丝不苟:

一个。 CellWDropdown(显示了单元格#1):您有一个单元格原型,其中包括图像、有问题的 3 个标签和一个用作下拉菜单并调用 UIPickerView 的按钮。

湾。 CellWStepper(单元格 #2 和 #3):您将拥有图像和 2 个标签,以及用于 +/- 按钮的所谓 UIStepper

c。 LastCell(如果是单元格,则为单元格#4?它也可以是视图):同样创建所需的 UI 元素并适当地约束它们。

  1. 注册您创建的 nib 文件:

tableView.register(UINib(nibName: "CellWDropDown", bundle: nil), forCellReuseIdentifier: "cellWDropdownIdentifier")
  1. 在 cellForRowAt 方法中,您调用上面创建的 XIB 以根据索引或所需的单元格类型制作单元格视图,例如:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
         let cell = tableView.dequeueReusableCell(withIdentifier: cellWDropdownIdentifier, for: indexPath) as! CellWDropdown
    }
    else if(indexPath.row == 1 || indexPath.row == 2){
         let cell = tableView.dequeueReusableCell(withIdentifier: cellWStepperIdentifier, for: indexPath) as! CellWStepper
    }
    //do other actions/styling
}
  1. 在您创建的 nib 和 UITableViewController 之间连接委托关系/方法。 这将是特定于应用程序的。

有很多细节需要填补空白,但这应该会对入门产生重大影响

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM