简体   繁体   English

使用包含CollectionView的自定义单元格重新加载单个TableViewCell

[英]Reload single TableViewCell with Custom Cell that includes CollectionView

I have a ViewController that has tableView. 我有一个具有tableView的ViewController。 That tableView populates Custom Cells. 该tableView填充自定义单元格。 Every Custom Cell has a collectionView that displays images (imagine news feed structure when every news block has images). 每个自定义单元格都有一个显示图像的collectionView(当每个新闻块都有图像时,想象新闻提要结构)。

Now, after editing anything in one news block, I want to refresh a single cell of that tableView (one news block). 现在,在一个新闻块中编辑了所有内容之后,我想刷新该tableView的单个单元格(一个新闻块)。 I tried to use [newsList reloadRowsAtIndexPaths: ... ] provided that I know exactly the indexPath of that tableView Cell, but the problem is that when I scroll down to the next tableView Cell and do editing there, after reloading that Second Cell all the images from the first news block will be shown in the second news block. 我尝试使用[newsList reloadRowsAtIndexPaths: ... ]前提是我确切知道该tableView Cell的indexPath,但是问题是当我向下滚动到下一个tableView Cell并在那里进行编辑后,重新加载了第二Cell来自第一个新闻块的图像将显示在第二个新闻块中。

Is there any other ways to refresh Custom Cell that includes CollectionView? 还有其他方法可以刷新包含CollectionView的自定义单元格吗?

Something that I found helpful to do with custom cells is to make a configureCell method and call that method whenever you give the cell new information. 我发现对自定义单元格有用的事情是创建一个configureCell方法,并在您向单元格提供新信息时调用该方法。 Also another thing to remember is to nil out custom data in prepareForReuse to help with reuse problems. 还要记住的另一件事是在prepareForReuse中取消自定义数据,以帮助解决重用问题。

class TestCell: UICollectionViewCell {
var myInfo: String? {
    didSet {
        if let myInfo = myInfo {
            configureCell(myInfo)
        }
    }
}

var label: UILabel?

//intialize the cell somewhere here

func configureCell(info: String) {
    label?.text = info
}

override func prepareForReuse() {
    super.prepareForReuse()
    label?.text = nil
}

} }

You can then get the cell at whatever index path and set the new info. 然后,您可以在任何索引路径下获取单元格并设置新信息。

Hopefully that can help you. 希望能对您有所帮助。

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

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