Skip to content

Commit f1a672d

Browse files
committed
Update readmy with ready-use section
1 parent e106aae commit f1a672d

File tree

2 files changed

+59
-32
lines changed

2 files changed

+59
-32
lines changed

Readme.md

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ If you want help project, check [Сooperation](#сooperation) section.
2121
- [Apply content](#apply-content)
2222
- [Mediator](#mediator)
2323
- [Sidebar](#sidebar)
24-
- [Ready Use Models](#ready-use-models)
24+
- [Ready Use](#ready-use)
25+
- [Example](#ready-use)
26+
- [Ready-use models](#ready-use-models)
2527
- [Сooperation](#сooperation)
2628
- [Other Projects](#other-projects)
2729
- [Russian Community](#russian-community)
@@ -64,26 +66,23 @@ If you prefer not to use any of dependency managers, you can integrate `SPDiffab
6466

6567
## Usage
6668

67-
Before read it, highly recomded check `Example` target in project. It examle show all features, like use stepper and switch, like process actions, create custom models and many other.
69+
Before read it, highly recomded check `Example` target in project. It examle show all features, like use stepper and switch, like process actions, create custom models and many other. Also you can skip full undestand logic and read [Ready-use section](#Ready Use) with minimum of code for start.
6870

69-
For work with diffable need create model (inside project you found some ready-use models) and do cell provider, which convert model with data to `UITableViewCell` or `UICollectionViewCell`. Next example for table, but all methods and class names available for collections.
71+
For work with diffable need create model (inside project you found some ready-use models) and do cell provider, which convert data-model to `UITableViewCell` or `UICollectionViewCell`. Next example for table, but all methods and class names available for collections.
7072

7173
New model shoud extend from basic class `SPDiffableItem`:
7274

7375
```swift
74-
class TableRowModel: SPDiffableItem {}
76+
class LocationRowModel: SPDiffableItem {}
7577
```
7678

7779
After it add properties, which you want use. For example:
7880

7981
```swift
80-
class TableRowModel: SPDiffableItem {
82+
class LocationRowModel: SPDiffableItem {
8183

82-
public var text: String
83-
public var detail: String? = nil
84-
public var icon: UIImage? = nil
85-
public var selectionStyle: UITableViewCell.SelectionStyle
86-
public var accessoryType: UITableViewCell.AccessoryType
84+
public var city: String
85+
public var adress: String?
8786
}
8887
```
8988

@@ -95,17 +94,15 @@ override func viewDidLoad() {
9594
super.viewDidLoad()
9695

9796
// Register cell for usage it in table view
98-
tableView.register(SPDiffableTableViewCell.self, forCellReuseIdentifier: SPDiffableTableViewCell.reuseIdentifier)
97+
tableView.register(LocationTableCell.self, forCellReuseIdentifier: "LocationTableCell")
9998

100-
// Cell provider for `TableRowModel`
101-
let cellProvider: SPDiffableTableCellProvider = { (tableView, indexPath, model) -> UITableViewCell? in
99+
// Cell provider for `LocationRowModel`
100+
let locationCellProvider: SPDiffableTableCellProvider = { (tableView, indexPath, model) -> UITableViewCell? in
102101
switch model {
103102
case let model as TableRowModel:
104-
let cell = tableView.dequeueReusableCell(withIdentifier: YourTableViewCell.identifier, for: indexPath) as! YourTableViewCell
105-
cell.textLabel?.text = model.text
106-
cell.detailTextLabel?.text = model.detail
107-
cell.accessoryType = model.accessoryType
108-
cell.selectionStyle = model.selectionStyle
103+
let cell = tableView.dequeueReusableCell(withIdentifier: "LocationTableCell", for: indexPath) as! LocationTableCell
104+
cell.textLabel?.text = model.city
105+
cell.detailTextLabel?.text = model.adress
109106
return cell
110107
default:
111108
return nil
@@ -114,11 +111,10 @@ override func viewDidLoad() {
114111

115112
// Pass cell provider and content.
116113
// About content you can read next.
117-
setCellProviders([cellProvider], sections: content)
114+
setCellProviders([locationCellProvider], sections: content)
118115
}
119116
```
120-
You can use default cell provider if using project's models. For get it call `SPDiffableTableController.defaultCellProvider`.
121-
All actions similar to collections. For example usage you can find in project in taget `Example`.
117+
In project available models for like `SPDiffableTableRow` and other with ready-use properties. Also you can use default cell provider if using project's models. For get it call `SPDiffableTableController.defaultCellProvider`.
122118

123119
### Apply Content
124120

@@ -131,11 +127,9 @@ let section = SPDiffableSection(
131127
header: SPDiffableTextHeaderFooter(text: "Header"),
132128
footer: SPDiffableTextHeaderFooter(text: "Footer"),
133129
items: [
134-
TableRowModel(text: "Basic Table Cell", accessoryType: .disclosureIndicator, action: { [weak self] indexPath in
135-
guard let self = self else { return }
136-
self.tableView.deselectRow(at: indexPath, animated: true)
137-
print("Tapped")
138-
})
130+
LocationRowModel(city: "Minsk", adress: "Frunze Pr., bld. 47, appt. 7"),
131+
LocationRowModel(city: "Shanghai", adress: "Ting Wei Gong Lu 9299long 168hao"),
132+
LocationRowModel(city: "London", adress: "94 Whitby Road")
139133
]
140134
)
141135

@@ -148,7 +142,7 @@ You can add more cells or sections. Last step - apply:
148142
diffableDataSource?.apply(sections: content, animating: true)
149143
```
150144

151-
That all. You can each time create new order or count cells and it automatically show with diffable animation. Project has some ready-use models, you can read about it next.
145+
That all. You can each time create new order or count cells and it automatically show with diffable animation.
152146

153147
### Mediator
154148

@@ -177,7 +171,7 @@ In protocol you can find more methods, like `canEdit` and other.
177171

178172
### Sidebar
179173

180-
Create new controller and extend from `SPDiffableSideBarController`. Remember, it available only from iOS 14. Now it abailable for `ios14` branch.
174+
Create new controller and extend from `SPDiffableSideBarController`. Remember, it available only from iOS 14.
181175

182176
```swift
183177
class SidebarController: SPDiffableSideBarController {}
@@ -209,7 +203,42 @@ SPDiffableSection(
209203
)
210204
```
211205

212-
## Ready Use Models
206+
## Ready Use
207+
208+
You can save time and count of code using ready-use classes. In project available models and views. For example you need simple table with native cells. You need create content with `SPDiffableTableRow`:
209+
210+
```swift
211+
let section = SPDiffableSection(
212+
identifier: "example section",
213+
header: SPDiffableTextHeaderFooter(text: "Header"),
214+
footer: SPDiffableTextHeaderFooter(text: "Footer"),
215+
items: [
216+
SPDiffableTableRow(text: "First Cell", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
217+
guard let self = self else { return }
218+
self.tableView.deselectRow(at: indexPath, animated: true)
219+
}),
220+
SPDiffableTableRow(text: "Second Cell", accessoryType: .disclosureIndicator, selectionStyle: .default, action: { [weak self] indexPath in
221+
guard let self = self else { return }
222+
self.tableView.deselectRow(at: indexPath, animated: true)
223+
}),
224+
]
225+
)
226+
```
227+
228+
You init cell model and pass action, choose selection style and other. As you see, model describe native table cell. Next, you need set cell provider, but it also already available, for get it call `SPDiffableTableController.defaultCellProvider`.
229+
230+
```
231+
setCellProviders([SPDiffableTableController.defaultCellProvider], sections: [section])
232+
```
233+
234+
Now project's models automatically converting to cell. No need any additional work. That all code.
235+
236+
For update table shoud using `apply()` method:
237+
```
238+
diffableDataSource?.apply(sections: [section], animating: true)
239+
```
240+
241+
## Ready-use models
213242

214243
It models which you can use now, it shoud close your task without code. Of couse you can create your models.
215244
Now in project you can find this ready-use models:
@@ -231,8 +260,6 @@ Now in project you can find this ready-use models:
231260

232261
#### For Collection:
233262

234-
Now in progress development.
235-
236263
- `SPDiffableSideBarItem` menu item in side bar. Support accessories and actions.
237264
- `SPDiffableSideBarButton` button item in side bar. Color of title similar to tint.
238265
- `SPDiffableSideBarHeader` header model for side bar item.

SPDiffable.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@
156156
OBJ_14 /* Models */ = {
157157
isa = PBXGroup;
158158
children = (
159+
OBJ_23 /* Table */,
159160
OBJ_15 /* Collection */,
160161
OBJ_19 /* SPDiffableItem.swift */,
161162
OBJ_20 /* SPDiffableSection.swift */,
162163
OBJ_21 /* SPDiffableSnapshot.swift */,
163164
OBJ_22 /* SPDiffableTextHeaderFooter.swift */,
164-
OBJ_23 /* Table */,
165165
);
166166
path = Models;
167167
sourceTree = "<group>";

0 commit comments

Comments
 (0)