Skip to content

Commit 67d2744

Browse files
author
GGsrvg
committed
add sample for UITableView
1 parent 09c06c3 commit 67d2744

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Field binding for MVVM
2020
`ControlProperty` is an object for tracking changes and inserting new values.
2121
`ControlAction` is an object for tracking actions.
2222

23-
### You can link objects in three ways:
23+
### You can bind objects in three ways:
2424
- -->: Data is only sent from the View.
2525
- <--: Data is only sent from the code.
2626
- <->: Data can be sent from View and code.
@@ -36,10 +36,49 @@ ViewController
3636
3737
override func viewDidLoad() {
3838
super.viewDidLoad()
39-
// linking object
39+
// binding object
4040
cancelContainer.activate([
4141
searchBar.searchTextField.observableText --> viewModel.observableSearch
4242
])
4343
}
4444
4545
```
46+
47+
### Bind UITableView and UICollectionView
48+
49+
ViewModel
50+
```
51+
let observableArray = ObservableArray<String>()
52+
```
53+
54+
ViewController
55+
```
56+
@IBOutlet weak var tableView: UITableView!
57+
58+
override func viewDidLoad() {
59+
super.viewDidLoad()
60+
// init adapter for UITableView
61+
// Pass a reference to `UITableView`
62+
// Pass the data source
63+
// And set the display of the cell.
64+
adapter = UITableViewClosureAdapter(
65+
tableView,
66+
observableDataSource: viewModel.observableArray,
67+
cellForRowHandler: { tableView, indexPath, row in
68+
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
69+
cell.textLabel?.text = row
70+
return cell
71+
},
72+
titleForHeaderSectionHandler: nil,
73+
titleForFooterSectionHandler: nil,
74+
numberOfSectionsHandler: nil,
75+
numberOfItemsInSectionHandler: nil
76+
)
77+
tableView.dataSource = adapter
78+
// Finally, pass the adapter to `CancelContainer` to clean up links automatically.
79+
cancelContainer.activate([
80+
adapter
81+
])
82+
}
83+
84+
```

0 commit comments

Comments
 (0)