UITableview在滚动时随机播放

时间:2018-02-03 18:09:10

标签: ios swift uitableview swift3 uiview

我有UITableView一些自定义视图,我正在成功添加所有内容。但是当我滚动tableview时,它会随着我在UIView上添加的那些UIImageView进行随机播放。你可以帮我解决这个问题。

请检查我的代码并给我正确的解决方案。请不要只选择swift3

if(tb_LayerType == "layer"){ 
        var cell = tableView.dequeueReusableCell(withIdentifier: "menuitem1", for: indexPath) as! MenuItemMultipleImageTableViewCell

        cell.tag = indexPath.row
        print(indexPath.row)

        cell.lblProductName.text = listSearch[indexPath.row].tb_product_name
        cell.lblProductTopings.text = listSearch[indexPath.row].tb_topping_name
        cell.selectionStyle = .none

        let productSize = listSearch[indexPath.row].tb_product_size!.components(separatedBy: ",")
        let productAmount = listSearch[indexPath.row].tb_product_amount!.components(separatedBy: ",")
      //  cell.imgView.kf.setImage(with: nil)

        if(productSize[0] == "regular"){
            cell.lblSize.text = "("+" Alm "+")"
            cell.lblPrice.text = productAmount[productSize.index(of: "regular")!]+" kr"
        }else if(productSize[0] == "small"){
            cell.lblSize.text = "("+" Lille "+")"
            cell.lblPrice.text = productAmount[productSize.index(of: "small")!]+" kr"
        }else{
            cell.lblSize.text = "( "+productSize[0].capitalized+" )"
            cell.lblPrice.text = productAmount[0]+" kr"
        }
        extraTopingImages.removeAll()
        extraTopingImages = listSearch[indexPath.row].tb_topping_image!.components(separatedBy: ",")
        for name in extraTopingImages {
            let image = UIImageView()
            self.UrlName = ""
            if let range = name.range(of: ".png") {
                let firstPart = name[name.startIndex..<range.lowerBound]
                self.UrlName = Constants.imageurl+firstPart+".png"
            }

            if let range = name.range(of: ".png") {
                let tagnumber = name[range.upperBound...]
                print(tagnumber)
                let url = URL(string: UrlName!)
                image.kf.setImage(with: url)
                image.frame = CGRect(x: 0, y: 0, width: 70, height: 70)
                //image.tag = Int(tagnumber)!
                image.layer.zPosition = CGFloat(Int(tagnumber)!)
                imageArr.append(image)

                cell.viewMultipleImage.addSubview(image)
            }
        }


         return cell
    }

1 个答案:

答案 0 :(得分:0)

当您使用dequeueReusableCell制作单元格对象时,dequeueReusable意味着单元格将一次又一次地重复使用。因此,在任何情况下,如果一个单元格找到if let range = name.range(of: ".png"),则所有其他单元格将使用相同的图像。

在这种情况下你应该做的是,记得在{1}}中将else关闭在cellForRow方法中。