Coder Social home page Coder Social logo

Comments (4)

nmdias avatar nmdias commented on May 21, 2024 1

Hey @bryzinski,

Ah! A UITextfield! Yeah, that's different. I use a Base class. Consider the following:

class BaseTableViewCell<ViewModelType>: UITableViewCell, Reusable {
    
    private var disposeBag: DisposeBag?
    
    var viewModel: ViewModelType? {
        didSet {
            guard let viewModel = viewModel else {
                return
            }
            let disposeBag = DisposeBag()
            self.configure(viewModel, disposedBy: disposeBag)
            self.disposeBag = disposeBag
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        configureDefaults()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public override func prepareForReuse() {
        super.prepareForReuse()
        self.disposeBag = nil
        self.viewModel = nil
    }
    
    func configureDefaults() {
        fatalError("Always override \(#function) to provide default view configurations.")
    }
    
    func configure(_ viewModel: ViewModelType, disposedBy disposeBag: DisposeBag) {
        fatalError("Always override \(#function) to provide view model configurations.")
    }
    
}

Now, with your own subclass, you could do this, maybe?

final class SomeTableViewCell: BaseTableViewCell<SomeTableViewCellViewModel> {
    
    let textfield: UITextField = {
        let textfield = UITextField()
        textfield.translatesAutoresizingMaskIntoConstraints = false
        // Do your thing
        return textfield
    }()
    
    override func configureDefaults() {
        // Do your thing
    }
    
    override func configure(_ viewModel: SomeTableViewCellViewModel, disposedBy disposeBag: DisposeBag) {
        // I suspect there might be a cleaner way to `drive` the subject...
        textfield.rx.text.asDriver().drive(onNext: { (string) in
            viewModel.text.on(.next(string))
        }).disposed(by: disposeBag)
    }
    
}

The cell has this corresponding ViewModelType:

struct SomeTableViewCellViewModel {
    let text = PublishSubject<String?>()
}

Now, when you instantiate your Cell's View Model, you can subscribe to it's text: PublishSubject<String?>

Something like:

// The Cell's View Model
viewModel.text.subscribe(onNext: { (string) in
    print(string)
}).disposed(by: disposeBag)

Then assign the Cell's viewModel

let cell = tableView.reusableCell() as SomeTableViewCell
cell.viewModel = viewModel
return cell

Does this help? Cheers

from cleanarchitecturerxswift.

nmdias avatar nmdias commented on May 21, 2024

Hi @bryzinski,

Have a look at the AllPosts scene in this repository. It does what you're describing. Ping me if you need help in understanding anything in particular.

Cheers

from cleanarchitecturerxswift.

ipaboy avatar ipaboy commented on May 21, 2024

Hi @nmdias thank you for your feedback. But unfortunately PostTableViewCell doesn't contain any UITextFields. To clarify I'm interested in a table view based form screen and looking for an elegant solution to pass rx streams of textfields (which are placed on cells) through view models's -transform method.

Probably the easiest way is to add Variable's to view model (like username, password) and bind them directly after cells are dequeued. But I love this transform function and still looking for the solution :)

from cleanarchitecturerxswift.

ipaboy avatar ipaboy commented on May 21, 2024

@nmdias thank you! It looks what I expected for.

from cleanarchitecturerxswift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.