swift - How can I display different products from an array in each custom cell? -
i need display 3 or less products array in every custom cell of tableview, putted 3 image views , 3 labels in each cell show products. tableviewdatasource code.
// mark: - table view data source override func numberofsections(in tableview: uitableview) -> int { // #warning incomplete implementation, return number of sections return products.count / 3 } override func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { // #warning incomplete implementation, return 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 }
and tableviewcell code.
protocol customcell : class { func accesstoproduct(product: product) 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 ui downloading product images , 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() }
}
however, if have array number of products not divisible 3, not correctly display products. imagine have 5 products, in first cell display 3 first products , in second cell display 2 remaining products , there 1 image view , 1 label empties. doesn't happen, code fills above 1 of products displayed. tried use collection view 3 cells y each tableview cell need cells of uicollectionview fixed , not cut edges, reason implemented 3 image views , 3 labels.
how can fix this?
Comments
Post a Comment