Comments (4)
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.
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.
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.
@nmdias thank you! It looks what I expected for.
from cleanarchitecturerxswift.
Related Issues (20)
- convert project to swift 4 HOT 2
- How to decode json other then using codable? HOT 4
- Any one can help me out? When i run the example project, it crashed "dyld: Library not loaded". HOT 8
- Why Post+Mapping.swift uses NSObject HOT 1
- save data list in realm HOT 1
- RxDataSources and CleanArchitecture
- Wrong Action when select "No" in Alert
- Generic UseCases and UseCaseProvider
- Communication with child view controller HOT 2
- Question around `do` and `Driver` for Input HOT 3
- How can I run your demo project on Xcode 10.1 & Swift 4.2 HOT 1
- You don't seem to understand Clean Architecture HOT 6
- crash on Xcode 11 when click Realm Tab HOT 2
- MVVM Isuue HOT 1
- Making use cases generic doesn't work with associatedtypes
- Multiple models for Repository in single UseCase. HOT 3
- Combine Version of ActivityIndicator and ErrorTacker HOT 1
- Maybe memory leaks
- Why use case containing rxSwift? HOT 1
- App crash when touched Realm tabbar HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cleanarchitecturerxswift.