【发布时间】: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