带有部分的UITableView在所有部分中重复单元格数据

问题描述

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

我正在尝试将部分标题添加到我的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]
 @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”)))”重复如下图所示:->

enter image description here

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

任何帮助将不胜感激。

解决方法

在每个节中,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
}
,

原因:

由于每个row中只有一个section,因此每次您使用indexPath.row作为第一行的0时,您最终都会访问{{1} },每个productarray[0]中的每个row

这就是所有行都相同的原因,因为所有行都用section填充。

解决方案:

只需使用productarray[0]代替indexPath.section

indexPath.row

注意:您无需创建3个不同的let data = productarray[indexPath.section] ,而是可以创建一个自定义类型的arrays,并将其用作{{ 1}}。 示例:

array

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

,

删除indexPath.row并使用indexPath.section。 当您使用indexPath.row时,它的第一项也是唯一项始终返回0。 因此您需要使用indexPath.sction

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