【问题标题】:TableView Items get refresh on scrollingTableView 项目在滚动时刷新
【发布时间】:2018-03-26 09:58:30
【问题描述】:

将 UICollectionView 作为 UITableView 行的子项。 UICollectionView 包含图像,但是每当我向下和向上滚动 tableview 时,集合视图图像就会随机消失。我附上图片以供我的问题参考。请建议我如何阻止这种情况。

我希望我的 tableview 是这样的。并且它的项目不应该在滚动时改变。

------------------------------------------ -------------------------------------------------- ------------------------------------------------强>

collectionview 图像在滚动 tableview 时消失。向上滚动后是这样的。

代码如下:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

    let cell:PartOfLookTableViewCell = self.looksListTable.dequeueReusableCell(withIdentifier: "cell") as! PartOfLookTableViewCell

    let oneRecord = looksArray[indexPath.row]

    cell.myCollectionView.loadInitial(_dataArray: oneRecord.imagesArray, isLooks: 1)

    return cell
}

将数据加载到 CollectionView 的代码:

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: looksReuseIdentifier, for: indexPath) as! CustomCollectionViewCell

          let oneRecord = inputArray[indexPath.row]

          cell.productImage.sd_setImage(with: URL.init(string: oneRecord.thumb_url)){ (image, error, cacheType, url) in

              DispatchQueue.main.async {
                  cell.productImage.image = image
              }

          }

        }        
   }
}

【问题讨论】:

  • 请添加您的代码
  • UITableViewCells 被重复使用,因此您需要在 CellForRowAtIndexPath 方法中设置图像
  • 您可以为图像应用标签并基于标签从服务器下载图像。
  • 对不起,我忘了添加代码。问题已编辑...
  • @Morti:感谢您的建议。粗体格式不是我做的。

标签: ios swift uitableview uicollectionview


【解决方案1】:

@Sourabh Bissa:

每当您的新单元格可见时,UITableView 都会使用方法 CellForRowAtIndexPath 重用单元格,您的此方法会重用数据源。

这里非常重要的是维护数据源:

在您的情况下,索引路径处的行的单元格将更新的值提供给集合视图方法,但您没有在主队列中重新加载。拿到数据源后马上尝试。

您在索引路径处的单元格将如下所示:

    guard let cell = self.tableview.dequeueReusableCell(withIdentifier: "cell") as! PartOfLookTableViewCell else {
        return UITableViewCell()
    }

    let oneRecord = looksArray[indexPath.row]

    cell.configureCell(for record : oneRecord with looks : 1)

    return cell

现在在单元格中,您将拥有集合视图出口,您将在其中实现集合视图数据源方法,并在那里异步下载图像。

Cell 类将如下所示:

  class PartOfLookTableViewCell: UITableViewCell {

 @IBOutlet weak var collectionView: UICollectionView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    collectionView.delegate = self
    collectionView.dataSource = self
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}


func configureCell(for record : Record , with looks : Int) {

    // Here reload your collection view

    // This collection view will be specific to the cell.

    collectionView.reloadData()
}
}

 extension PartOfLookTableViewCell : UICollectionViewDelegate ,  UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    //return  array
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    // Asyncronously download images
}
}

这是您无需使用任何标签即可实现您的要求的方法。如果您有任何疑问,请告诉我。

【讨论】:

    猜你喜欢
    • 2012-09-08
    • 1970-01-01
    • 2015-06-10
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    相关资源
    最近更新 更多