我需要在tableview的每个自定义单元格中显示三个或更少的数组产品,我在每个单元格中放置了三个图像视图和三个标签来显示产品。这是我的TableviewDataSource代码。
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return products.count / 3
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeCell", for: indexPath) as! HomeTableViewCell
let sectionIndex = indexPath.section
if (sectionIndex + index + 2) <= products.count && (sectionIndex + index + 1) <= products.count && currentUser != nil {
cell.product1 = self.products[sectionIndex + index]
cell.product2 = self.products[sectionIndex + index + 1]
cell.product3 = self.products[sectionIndex + index + 2]
cell.selectionStyle = .none
index += 2
}
cell.delegate = self
return cell
}
这是我的TableviewCell代码。
协议CustomCell:class { func accessToProduct(产品:产品) func performSegueToProduct() }
class HomeTableViewCell:UITableViewCell {
@IBOutlet weak var image1 : UIImageView!
@IBOutlet weak var image2 : UIImageView!
@IBOutlet weak var image3 : UIImageView!
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var label3: UILabel!
weak var delegate: CustomCell?
var product1: Product! {
didSet {
updateUI1()
}
}
var product2: Product! {
didSet {
updateUI2()
}
}
var product3: Product! {
didSet {
updateUI3()
}
}
var cache = SAMCache.shared()
func downloadPopularImages (product: Product, imageView: UIImageView, label: UILabel) {
imageView.image = nil
let productuid = product.uid
let profileImageKey = "\(productuid)"
if let image = cache?.object(forKey: profileImageKey) as? UIImage {
imageView.image = image
} else {
product.downloadPopularProductImage { [weak self] (image, error) in
if let error = error {
print(error.localizedDescription)
} else {
imageView.image = image
self?.cache?.setObject(image, forKey: profileImageKey)
}
}
}
}
// MARK: - Update the UI downloading product images and adding tapGestureRecognizer
func updateUI1() {
downloadPopularImages(product: product1, imageView: image1, label: label1)
label1.text = product1.name + " \(product1.subName)"
let tapGestureRecognizer1 = UITapGestureRecognizer(target: self, action: #selector(self.image1IsTapped))
image1.addGestureRecognizer(tapGestureRecognizer1)
}
func image1IsTapped () {
delegate?.accessToProduct(product: product1)
print(product1)
delegate?.performSegueToProduct()
}
func updateUI2() {
downloadPopularImages(product: product2, imageView: image2, label: label2)
label2.text = product2.name + " \(product2.subName)"
let tapGestureRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(self.image2IsTapped))
image2.addGestureRecognizer(tapGestureRecognizer2)
}
func image2IsTapped() {
delegate?.accessToProduct(product: product2)
delegate?.performSegueToProduct()
}
func updateUI3() {
downloadPopularImages(product: product3, imageView: image3, label: label3)
label3.text = product3.name + " \(product3.subName)"
let tapGestureRecognizer3 = UITapGestureRecognizer(target: self, action: #selector(self.image3IsTapped))
image3.addGestureRecognizer(tapGestureRecognizer3)
}
func image3IsTapped() {
delegate?.accessToProduct(product: product3)
delegate?.performSegueToProduct()
}
}
但是,如果我的阵列中有许多产品不能被3整除,则无法正确显示所有产品。想象一下,我们有5个产品,所以在第一个单元格中它将显示三个第一个产品,在第二个单元格中它将显示剩下的两个产品,并且将有一个图像视图和一个标签清空。但这不会发生,代码用已经显示的产品之一填充上面的内容。 我尝试使用每个tableview单元格的三个单元格的集合视图,但我需要修复uicollectionview的单元格而不是边缘切割,因此我实现了三个图像视图和三个标签。
我该如何解决这个问题?