【问题标题】:UITableView with Sections Repeating cell data in all the sectionsUITableView with Sections 在所有部分中重复单元格数据
【发布时间】:2020-09-16 10:54:04
【问题描述】:

我知道以前有人问过同样的问题,但没有帮助。

我正在尝试将部分标题添加到我的 UITableView。我能够创建部分并正确计算每个部分中的元素数量,单元格与所有部分中的数据重复。

我只发布相关代码 -

我的模型是 -

 import UIKit
 struct Product:Equatable {
  let imagename: UIImage }

  var productarray = [Product(imagename:#imageLiteral(resourceName: "CakeImage")),
                Product( imagename:#imageLiteral(resourceName: "PeasImge")),Product(imagename:#imageLiteral(resourceName: "vectorlogo")),
                Product(imagename: #imageLiteral(resourceName: "blue"))]

ProductViewController 是 -

 import UIKit

 class ProductViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
 let sections = ["Section A", "Section B","Section C", "Section D"]
 let rowspersection = [1, 1,1,1]
 @IBOutlet weak var tableView: UITableView!
 override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self   
 }
 func numberOfSections(in tableView: UITableView) -> Int {
   return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return rowspersection[section]
    }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let data = productarray[indexPath.row]
     let cell = tableView.dequeueReusableCell(withIdentifier: "ProductTableViewCell") as! ProductTableViewCell
     cell.imageView?.image =  data.imagename
     cell.myParent = self
       return cell
  }

   func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 44
}
   func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    switch(section) {
    case 0:return "Section A"
     case 1:return "Section B"
    case  2:return "Section C"
    case  3 :return "Section D"
    default :return ""
        
    }
  }
}

现在,在上面只有 productarray 的第一个图像,即“[Product(imagename:#imageLiteral(resourceName:“CakeImage”))”在所有部分中重复,如下图所示:-

我希望所有图像/单元格都在相应的部分中,而不仅仅是一个图像/单元格在所有部分中重复。

任何帮助将不胜感激。

【问题讨论】:

    标签: ios swift uitableview sections


    【解决方案1】:

    原因:

    由于您在每个 section 中都有一个 row,因此每次您在第一行使用 indexPath.row(即 0)时,您最终会在每个 @987654327 中为每个 row 访问 productarray[0] @。

    这就是所有行都相同的原因,因为它们都用productarray[0]填充。

    解决方案:

    只需使用indexPath.section 而不是indexPath.row

    let data = productarray[indexPath.section]
    

    注意:您可以创建一个自定义类型的array 并将其用作tableViewdataSource,而不是创建3 个不同的arrays示例:

    struct Section {
        let name: String
        let products: [Product]
    }
    
    let sections = [
        Section(name: "Section A", products: [Product(imagename:#imageLiteral(resourceName: "CakeImage"))]),
        Section(name: "Section B", products: [Product(imagename:#imageLiteral(resourceName: "PeasImge"))]),
        Section(name: "Section C", products: [Product(imagename:#imageLiteral(resourceName: "vectorlogo"))]),
        Section(name: "Section D", products: [Product(imagename:#imageLiteral(resourceName: "blue"))]),
    ]
    

    使用sections array 作为数据源。这样可以避免很多混乱。

    【讨论】:

    • 好的,是的,当我将其更改为 indexpath.section 时,它可以工作,但我已经向您展示了最简单的代码。如果在上面的 ProductViewController 中说,那会怎样,修改 - 让sections = [“Section A”,“Section B”] & let rowspersection = [2,2] 即 2 张图片,每张图片只有 2 个 ssections 请注意,我没有想换模型是因为真实代码比较多。请调查我的问题。我会负债累累的。
    • 嗯,这只是基于您当前代码的建议。无论如何很高兴问题得到解决。快乐编码?
    • 如果答案有效,请接受,这样其他人就不会花时间解决它。
    • @是的,问题已得到解答,但恐怕原来的问题还没有完全解决。我已经向你展示了最简单的代码。请阅读我上面的评论。
    • 我做到了。您需要针对其他问题提出单独的问题。当原始问题中存在的问题陈述得到解决时,该问题被视为已回答。
    【解决方案2】:

    在每个部分 indexPath 都从零开始,因此它显示 productarray 的第一个索引。

    let data = productarray[indexPath.row]
    

    替换为

    let data = productarray[indexPath.row + indexPath.section]
    

    编辑

    var index = indexPath.row
    if indexPath.section != 0, rowspersection.count > indexPath.section - 1{
       index += rowspersection[indexPath.section - 1] 
    }
    if index < productarray.count{
      let data = productarray[index]
      cell.imageView?.image =  data.imagename
    }
    

    编辑结帐 替换此方法

    func updateCart(cell: ProductTableViewCell) {
        guard let indexPath = tableView.indexPath(for: cell) else { return }
        var index = indexPath.row
        if indexPath.section != 0, rowspersection.count > indexPath.section - 1{
            index += rowspersection[indexPath.section - 1]
        }
        
        if index < productarray.count{
            let product = productarray[index]
            
            //Update Cart with product
            cart.updateCart(with: product)
            self.navigationItem.rightBarButtonItem?.title = "Checkout (\(cart.items.count))"
        }
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell   {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ProductTableViewCell") as!   ProductTableViewCell
        cell.delegate = self // original issue was here, now resolved.
        
        var index = indexPath.row
        if indexPath.section != 0, rowspersection.count > indexPath.section - 1{
            index += rowspersection[indexPath.section - 1]
        }
        
        if index < productarray.count{
            let data = productarray[index]
            cell.name?.text = data.name
            cell.imageView?.image =  data.imagename
            
            let product = productarray[index]
            cell.setButton(state: self.cart.contains(product: product))
        }
        
        return cell
    }
    

    【讨论】:

    • -当我遵循您的代码时,我只在 2 个部分中制作 2 个图像,即 let sections = ["Section A", "Section B"] & let rowspersection = [2,2] ,第一部分(A 部分)的第二个图像/单元格在第二个部分(B 部分)的第一个图像/单元格中重复,该部分(B 部分)的原始第一个图像/单元格无处可寻。所以,预期的图像 - A 部分 - CakeImage、PeasImge 和 B 部分 - vectorlogo、蓝色 意想不到的图像 - A 部分 - CakeImage、PeasImge 和 B 部分 - PeasImge、蓝色 如何排序?注意 - 我不想更改模型。
    • 是的,您编辑的答案运行良好。赞成。谢谢。
    • 好的,有一个绊脚石 - 当我点击第一部分的一行的添加按钮时,说“a部分”它添加并且 Checkout(0) 变为 1,即 Checkout(1)。到这里为止,没关系,但是当我单击另一个部分的一行的添加按钮时,比如“部分 b”,Checkout(1) 再次变为 0,即它变为 Checkout(0)。怎么整??
    • @askit 请正确分享代码和您的要求,以便我理解
    • 完整代码在这里-stackoverflow.com/a/64012036/7444874
    猜你喜欢
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多