London Escorts sunderland escorts 1v1.lol unblocked yohoho 76 https://www.symbaloo.com/mix/yohoho?lang=EN yohoho https://www.symbaloo.com/mix/agariounblockedpvp https://yohoho-io.app/ https://www.symbaloo.com/mix/agariounblockedschool1?lang=EN
-2.9 C
New York
Sunday, February 2, 2025

ios – 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 info within the assortment view, it jumps again to the primary cell, which is undesirable for my consumer 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 would like to remain at cell which I click on and replace solely that cell.

Right here is connected video with downside:
https://youtube.com/shorts/JlsLCepdNU8?function=share

Here is what I’ve tried thus far:

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

struct DataModel: Hashable {
    var merchandise: [Product]
    var retailer: [String]
    func hash(into hasher: inout Hasher) {
        hasher.mix(merchandise)
        hasher.mix(retailer)
    }
    
    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() {
        tremendous.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
            cell.configure(merchandise)
            return cell
        })
        comfigureUI()
        reloadData(knowledge)
    }
    
    personal func reloadData(_ knowledge: DataModel) {
        var snapshot = NSDiffableDataSourceSnapshot<Part, DataModel>()
        snapshot.appendSections([.one])
        snapshot.appendItems([data])
        datasource?.apply(snapshot, animatingDifferences: false)
    }
}

extension ViewController: ClickDelegate {
    func click on(_ product: Product) {
        retailer.append(prodcut.id)
        reloadData(.init(merchandise: prodcuts, retailer: retailer))
    }
}
class TableViewCell: UITableViewCell {
    
    weak var delegate: ClickDelegate?
    
    var collectionView: UICollectionView = {
        let format = UICollectionViewFlowLayout()
        format.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
        format.scrollDirection = .horizontal
        format.minimumInteritemSpacing = 8
        let c = UICollectionView(body: .zero, collectionViewLayout: format)
        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
        })
        comfigureUI()
        
        backgroundColor = .pink
        layer.cornerRadius = 8
    }
    
    func configure(_ knowledge: DataModel) {
        self.knowledge = knowledge
        reloadData(knowledge.merchandise)
    }
    
    personal func reloadData(_ merchandise: [Product]) {
        var snapshot = NSDiffableDataSourceSnapshot<Part, Product>()
        snapshot.appendSections([.one])
        snapshot.appendItems(merchandise)
        datasource?.apply(snapshot, animatingDifferences: false)
    }
    
    personal func comfigureUI() {
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints {
            $0.edges.equalToSuperview()
            $0.top.equalTo(200)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been carried out")
    }
}

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(product.id) ? "chosen" : "add", for: .regular)
    }
    
    override init(body: CGRect) {
        tremendous.init(body: .zero)
        backgroundColor = .cyan
        layer.cornerRadius = 8
        configureUI()
    }
    
    @objc func click on() {
        guard let product = product else { return }
        delegate?.click on(product)
    }
    
    func configureUI() {
        
        contentView.snp.makeConstraints {
            $0.top.equalTo(200)
            $0.width.equalTo(150)
        }
        
        contentView.addSubview(label)
        contentView.addSubview(button)
        
        label.snp.makeConstraints {
            $0.middle.equalToSuperview()
        }
        
        button.snp.makeConstraints {
            $0.centerX.equalToSuperview()
            $0.backside.equalToSuperview().offset(-8)
        }
    }
}

Related Articles

Social Media Auto Publish Powered By : XYZScripts.com