简体   繁体   中英

Reload single TableViewCell with Custom Cell that includes CollectionView

I have a ViewController that has tableView. That tableView populates Custom Cells. Every Custom Cell has a collectionView that displays images (imagine news feed structure when every news block has images).

Now, after editing anything in one news block, I want to refresh a single cell of that tableView (one news block). 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.

Is there any other ways to refresh Custom Cell that includes 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. Also another thing to remember is to nil out custom data in prepareForReuse to help with reuse problems.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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