You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:TableView未显示正确图片,ViewController传参异常

Hey there, let's break down this issue you're facing step by step. You've got ViewController A (with a CollectionView that loads all images) and ViewController B (TableView-based, showing a single image passed from A). It works on first launch, but after adding new images to A's CollectionView, tapping the new one sends the right postImg data (per your logs) but B displays the wrong image. Here are the most likely fixes:

Common Causes & Solutions

1. TableView Cell Reuse Gotcha

Since B uses a TableView, cell reuse is the #1 suspect here. If you don't properly reset or update the cell's image every time it's loaded, the reused cell will show old content even if your postImg is correct.

Fix:

  • Always explicitly set (and reset) the image in cellForRowAt, no matter if the cell is new or reused:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "YourImageCellID", for: indexPath) as! YourImageCell
    // Reset first to clear reused content
    cell.imageView?.image = nil
    // Load the correct image from postImg
    if let targetImage = postImg {
        cell.imageView?.image = targetImage
    }
    return cell
}
  • Refresh the TableView every time B appears to ensure it loads the latest data:
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    tableView.reloadData()
}

2. Async Image Loading Race Condition

If your postImg is loaded asynchronously (e.g., from a cache or network), old pending requests might finish after the new image is supposed to load, overwriting the correct content.

Fix:

  • Cancel any ongoing image requests when reusing cells or updating the image. For example, if using Kingfisher:
// Cancel prior tasks first
cell.imageView?.kf.cancelDownloadTask()
// Load the new image (adjust if you're passing a URL instead of UIImage)
if let imageURL = postImgURL {
    cell.imageView?.kf.setImage(with: imageURL)
} else if let image = postImg {
    cell.imageView?.image = image
}
  • If you're handling async loading manually, add logic to cancel tasks when the cell is reused or the ViewController disappears.

3. Data Update Trigger Missing

Even if postImg is correctly passed, if you only set the image once (like in viewDidLoad), B won't update if the data arrives after that lifecycle point.

Fix:

  • Add a property observer to postImg to auto-refresh the TableView when it changes:
var postImg: UIImage? {
    didSet {
        tableView.reloadData()
    }
}
  • Double-check your segue preparation in A to ensure you're passing the correct image from the updated array:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    guard segue.identifier == "SegueToB",
          let selectedIndex = collectionView.indexPathsForSelectedItems?.first?.item,
          let destinationVC = segue.destination as? ViewControllerB else { return }
    // Make sure your image array is updated with the new images before this point!
    destinationVC.postImg = yourImageCollection[selectedIndex]
}

4. Cache Conflicts

If you're loading images via URLs, new images might have duplicate URLs or your cache isn't updating, leading to old images being displayed.

Fix:

  • Verify new images have unique URLs. For testing, temporarily disable caching (e.g., with SDWebImageOptions.refreshCached if using SDWebImage) to rule out cached content issues.

Start with checking the TableView reuse logic first—since your logs confirm postImg is correct, the problem is almost certainly in how B renders that data.

内容的提问来源于stack exchange,提问作者Alvin123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:43:08