Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UITableView中重用单元格_Ios_Swift_Uitableview_Prepareforreuse - Fatal编程技术网

Ios 在UITableView中重用单元格

Ios 在UITableView中重用单元格,ios,swift,uitableview,prepareforreuse,Ios,Swift,Uitableview,Prepareforreuse,我有我的自定义单元格“NewsCell”。它包含我的自定义视图“ImageMosaicView”(这只是UIView的子类)。我用它来显示照片,就像在Facebook上发布的一样。我只是将图像的URL传递给ImageMosaicView的实例,然后它加载并显示 我有个问题。当我快速滚动tableView时,在加载新图像的同时,上一个单元格中的图像会在新单元格中出现一段时间。但我不知道它们是如何出现的,因为我提供了默认图像 我怎样才能避免这种情况 // ImageMosaicView.swift

我有我的自定义单元格“NewsCell”。它包含我的自定义视图“ImageMosaicView”(这只是UIView的子类)。我用它来显示照片,就像在Facebook上发布的一样。我只是将图像的URL传递给ImageMosaicView的实例,然后它加载并显示

我有个问题。当我快速滚动tableView时,在加载新图像的同时,上一个单元格中的图像会在新单元格中出现一段时间。但我不知道它们是如何出现的,因为我提供了默认图像

我怎样才能避免这种情况

// ImageMosaicView.swift

class ImageMosaicView: UIView {
    @IBOutlet var imageViews: [UIImageView]!

    var urlStrings: [String] = [] {
    didSet {
        for imageView in imageViews {
            if let url = URL(string: urlStrings[i]) {
                imageView.loadImage(url: url)
            }
        }
    }

    // MARK: - Initialization

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        let _ = loadViewFromNib()
    }

    // MARK: - Methods

    func loadViewFromNib() -> UIView {
        let bundle = Bundle.init(for: type(of: self))
        let nib = UINib(nibName: "ImageMosaicView", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
        view.frame = bounds
        view.autoresizingMask = [
            UIViewAutoresizing.flexibleWidth,
            UIViewAutoresizing.flexibleHeight
        ]
        addSubview(view)
        return view
    }
}

// How I provide urls for images:

// NewsViewController.swift. 'tableView(cellForRow:, indexPath:)

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let news = newsCollection[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "NewsCell", for: indexPath) as! NewsCell
        //...
        cell.imageMosaicView.urlStrings = news.imageUrlsCollection
        //...
        return cell
    } else {
        return UITableViewCell()
    }
}

正确的方法是在
NewsCell
类中配置
prepareforuse()
方法

override func prepareForReuse() {
    super.prepareForReuse()

    imageView.image = //Default image
    //Whatever other things you need cleared.
}
每当tableView将单元格出列时,就会调用此方法。因此,任何缓存的数据都应该在此处清除,否则它将保留在已退出队列的单元格中,除非在
cellForRowAt
方法中返回之前手动更改它