简体   繁体   English

单元重用tableview内collectionview中的另一个图像

[英]cell reuse another image in collectionview inside tableview

Currently, 目前,

I have a feed for the user and it uses uitableview and there is a uicollectionview inside tableview for multi-images. 我为用户提供了一个供稿,它使用uitableview,并且tableview内有一个uicollectionview用于多图像。

'Like' or 'Comment' functions are working well but some issues happen when user taps 'Like'. “赞”或“评论”功能运行良好,但是当用户点击“赞”时会发生一些问题。

I do not want to show several changes, but when user taps 'Like', I need to reload a cell and it shows another picture for a short time(bcs of reuse) and back to original image. 我不想显示一些更改,但是当用户点击“赞”时,我需要重新加载单元格,并且它会在短时间内显示另一张图片(重复使用的bcs),并返回到原始图像。

I tried to use the function, prepareForReuse(). 我试图使用函数prepareForReuse()。 However, I am not sure how to maintain the same image currently on the screen when they are reloading. 但是,我不确定在重新加载时如何在屏幕上保持当前的图像。 Any idea u have? 你有什么主意吗?

For ur more information, let me show my tableview's part of 'CellForItemAt' and collectionview's same method. 有关更多信息,请允许我显示“ CellForItemAt”的表视图部分和collectionview的相同方法。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "storyCell", for: indexPath) as! StoryPhotoCollectionViewCell

    let imageURL = photoList[indexPath.row]
    let url = URL(string: imageURL)

    DispatchQueue.main.async {
        cell.imageView.kf.setImage(with: url, placeholder: #imageLiteral(resourceName: "imageplaceholder"), options: nil, progressBlock: nil, completionHandler: nil)
    }

    cell.imageView.kf.indicatorType = .activity

    return cell
}

The collectionView's datasource is photoList array, so in the tableview, I have this code. collectionView的数据源是photoList数组,因此在tableview中,我有此代码。

DispatchQueue.main.async { 
        cell.photoList = (story.imageArray?.components(separatedBy: ",").sorted())!
    }

The issue should be due to delay in asynchronous download of the image, 该问题应归因于图片异步下载的延迟,

I believe your code would be like this 我相信你的代码会像这样

 cell.image = downloadFromURL(SOME_URL)

Add this single line code before assigning the image 在分配图像之前添加此单行代码

cell.image = nil; // or some placeholder image
cell.image = downloadFromURL(SOME_URL)

If possible please provide more info 如果可能,请提供更多信息

If I understand well your question, you need: 如果我了解您的问题,那么您需要:

  1. When the status change, you change the image so, this image can be stored as part of the app or external. 状态更改时,您可以更改图像,因此可以将该图像存储为应用程序的一部分或外部。 If it is the same image, you need to keep it cached. 如果是同一图像,则需要对其进行缓存。
  2. You start a timer like: 您启动一个计时器,如:

    let countTimeBeforeChangeMyImageBack = DispatchTime.now() + .seconds(waitTimeBeforeStartCounting) 让countTimeBeforeChangeMyImageBack = DispatchTime.now()+ .seconds(waitTimeBeforeStartCounting)

    DispatchQueue.main.asyncAfter(deadline: deadlineTime, execute: { self.YourMethodToChangeYourImageBack }) DispatchQueue.main.asyncAfter(最后期限:最后期限,执行:{self.YourMethodToChangeYourImageBack})

  3. At your method YourMethodToChangeYourImageBack you set the original image back. 在您的方法YourMethodToChangeYourImageBack上,您将原始图像设置回了。 Here, remember you could need to join back to the main thread before updating the UI: 在这里,请记住,在更新UI之前,您可能需要重新加入主线程:

    // Re join the main queue after the callback DispatchQueue.main.sync() { // Your code to update the image } //在回调DispatchQueue.main.sync()之后重新加入主队列{//您的代码以更新图像}

Hope it Helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM