UITableView with Sections 在所有部分重复单元格数据 [英] UITableView with Sections Repeating cell data in all the sections

查看:23
本文介绍了UITableView with Sections 在所有部分重复单元格数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I Know same question has been asked before , but it did not help.

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

I am trying to add section titles to my UITableView. I am able to create the sections and count the number of elements in each section properly, , the cells are repeated the data in all the sections.

我只发布相关代码 -

我的模型是 -

 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 是 -

The ProductViewController is -

 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")),"在所有部分中重复,如下图所示:-

Now, in the above only the first image of the productarray i.e. "[Product(imagename:#imageLiteral(resourceName: "CakeImage"))," is repeated in all the sections as shown in the image below:-

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

I want all the images/cell to be in the respective sections and not just one image/cell to be repeated in all the sections.

任何帮助将不胜感激.

推荐答案

在每个部分的 indexPath 都以零开头,所以它显示了 productarray 的第一个索引.

in every section indexPath start with zero so it display first index of 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
}

这篇关于UITableView with Sections 在所有部分重复单元格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆