Thursday, October 29, 2026
HomeiOS Developmentios - replace information at cell in MVVM?

ios – replace information at cell in MVVM?

[ad_1]

I am making a Twitter clone app.
If I press the like button on the posts in every cell, I wish to replicate the variety of likes to extend. Additionally, I wish to make it replicate within the cell as quickly as I delete or modify the put up. There’s additionally a video participant within the cell like Twitter, however each time I press the like button, the ViewModel accepts once more, which causes the video that was operating to show off. Please inform me one of the simplest ways to implement the Like button…

Cell.swift

func bind(_ viewModel: PostItemViewModel) {
    .....
    likesLabel.textual content = String(viewModel.likes)
    ....
}
....
likesBtn.rx.tapGesture().when(.ended).mapToVoid().asSignalOnErrorJustComplete()
    .emit(onNext: { [unowned self] in
        delegate?.pressLikesBtn(tag)
    }).disposed(by: disposeBag)

ViewController.swift

class PostsViewController: UIViewController {
    non-public let pressLikesRelay = PublishRelay<Int>()
    non-public let deleteRelay = PublishRelay<Int>()
    non-public let disposeBag = DisposeBag()

    var viewModel: PostsViewModel!
    .......
    .......

    non-public func bindViewModel() {
    assert(viewModel != nil)
    let viewWillAppear = rx.viewWillAppear
        .mapToVoid()
        .asSignalOnErrorJustComplete()
    let pull = timeLineTableView.refreshControl!.rx
        .controlEvent(.valueChanged)
        .asSignal()
    let reachedBottom = timeLineTableView.rx.reachedBottom().asSignal()
    
    let enter = PostsViewModel.Enter(fetchInitial: Sign.merge(viewWillAppear, pull), fetchNext: reachedBottom, likesTrigger: pressLikesRelay.asSignal(), deleteTrigger: deleteRelay.asSignal())
    let output = viewModel.remodel(enter: enter)
    
    output.posts.drive(timeLineTableView.rx.gadgets(cellIdentifier: PostTableViewCell.identifier, cellType: PostTableViewCell.self)) {
        row, merchandise, cell in
        cell.delegate = self
        cell.bind(merchandise)
        cell.tag = row
    }.disposed(by: disposeBag)
}

extension PostsViewController: PostTableCellDelegate {
    func pressLikesBtn(_ index: Int) {
        pressRelay.settle for(index)
    }

    func deletePost(_ index: Int) {
        deleteRelay.settle for(index)
    }
}

ViewModel.swift

closing class PostsViewModel: ViewModelType {
    ...
    non-public let postsRelay = BehaviorRelay<[PostItemViewModel]>(worth: [])
    .....

    func remodel(enter: Enter) -> Output {
        .......
            
        enter.likesTrigger.emit(onNext: { [unowned self] index in
            var up to date = self.postsRelay.worth
            up to date[index].plusLikes()
            self.postsRelay.settle for(up to date)
        }).disposed(by: disposeBag)
        
        enter.deleteTrigger.emit(onNext: { [unowned self] index in
            var deleted = self.postsRelay.worth
            deleted.take away(at: index)
            self.postsRelay.settle for(deleted)
        }).disposed(by: disposeBag)
            
        return Output(
        ...,
            posts: postsRelay.asDriver(),
            ...)
    }

}

PostItemViewModel.swift

public struct PostItemViewModel {
    .....
    var likes: Int
    ....

    mutating func plusLikes() {
        self.vote1 +=1
    }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments