iOS Development

ios – Find out how to Reload a Horizontal Assortment View Inside a TableViewCell With out Leaping to the First Cell?

I’ve a horizontal UICollectionView embedded inside a UITableViewCell. After I reload the information within the assortment view, it jumps again to the primary cell, which is undesirable for my person expertise.

Is there a method to reload the gathering view with out inflicting it to leap again to the primary cell? I’ve tried a number of approaches, however none appear to work easily.

After I click on so as to add button inside CollectionViewCell all collections reloads and jumps again to first cell. However I want to remain at cell which I click on and replace solely that cell.

Right here is hooked up video with downside:

Here is what I’ve tried up to now:

struct Product: Hashable {
    var id: String
    var value: Int
    var isSelected: Bool = false

struct DataModel: Hashable {
    var merchandise: [Product]
    var retailer: [String]
    func hash(into hasher: inout Hasher) {
    static func == (lhs: DataModel, rhs: DataModel) -> Bool {
        lhs.hashValue == rhs.hashValue
class ViewController: UIViewController {
    lazy var tableView: UITableView = {
        let desk = UITableView()
        desk.rowHeight = UITableView.automaticDimension
        desk.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
        return desk
    var retailer = [String]()
    var datasource: UITableViewDiffableDataSource<Part, DataModel>?
    var prodcuts = [Product(id: "131234", price: 14343),
                    Product(id: "1231232", price: 234523),
                    Product(id: "435345", price: 23433),
                    Product(id: "4353445", price: 12321338)]
    lazy var knowledge = DataModel(merchandise: prodcuts, retailer: [])
    override func viewDidLoad() {
        datasource = UITableViewDiffableDataSource<Part, DataModel>(tableView: tableView, cellProvider:  { (tableView, indexPath, merchandise) -> UITableViewCell? in
            let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
            cell.delegate = self
            return cell
    personal func reloadData(_ knowledge: DataModel) {
        var snapshot = NSDiffableDataSourceSnapshot<Part, DataModel>()
        datasource?.apply(snapshot, animatingDifferences: false)

extension ViewController: ClickDelegate {
    func click on(_ product: Product) {
        reloadData(.init(merchandise: prodcuts, retailer: retailer))
class TableViewCell: UITableViewCell {
    weak var delegate: ClickDelegate?
    var collectionView: UICollectionView = {
        let structure = UICollectionViewFlowLayout()
        structure.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
        structure.scrollDirection = .horizontal
        structure.minimumInteritemSpacing = 8
        let c = UICollectionView(body: .zero, collectionViewLayout: structure)
        c.backgroundColor = .yellow
        c.register(CollectionCell.self, forCellWithReuseIdentifier: "CollectionCell")
        return c
    var datasource: UICollectionViewDiffableDataSource<Part, Product>?
    var knowledge: DataModel?
    override init(type: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        tremendous.init(type: type, reuseIdentifier: reuseIdentifier)
        datasource = UICollectionViewDiffableDataSource<Part, Product>(collectionView: collectionView, cellProvider: { (collectionView, indexPath, merchandise) -> UICollectionViewCell? in
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
            cell.delegate = self.delegate
            guard let knowledge = self.knowledge else { return UICollectionViewCell() }
            cell.configure(knowledge.merchandise[indexPath.row], retailer: knowledge.retailer)
            return cell
        backgroundColor = .purple
        layer.cornerRadius = 8
    func configure(_ knowledge: DataModel) {
        self.knowledge = knowledge
    personal func reloadData(_ merchandise: [Product]) {
        var snapshot = NSDiffableDataSourceSnapshot<Part, Product>()
        datasource?.apply(snapshot, animatingDifferences: false)
    personal func comfigureUI() {
        collectionView.snp.makeConstraints {
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been applied")

protocol ClickDelegate: AnyObject {
    func click on(_ product: Product)

class CollectionCell: UICollectionViewCell {
    weak var delegate: ClickDelegate?
    var product: Product?
    var label = UILabel()
    lazy var button: UIButton = {
        let bttn = UIButton()
        bttn.setTitle("add", for: .regular)
        bttn.addTarget(self, motion: #selector(click on), for: .touchUpInside)
        return bttn
    func configure(_ product: Product, retailer: [String]) {
        self.product = product
        button.setTitle(retailer.comprises( ? "chosen" : "add", for: .regular)
    override init(body: CGRect) {
        tremendous.init(body: .zero)
        backgroundColor = .cyan
        layer.cornerRadius = 8
    @objc func click on() {
        guard let product = product else { return }
        delegate?.click on(product)
    func configureUI() {
        contentView.snp.makeConstraints {
        label.snp.makeConstraints {
        button.snp.makeConstraints {


Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button