Skip to content

Commit ec0ba98

Browse files
committed
支持刷新数据源更新cell的功能
1 parent bfd7899 commit ec0ba98

21 files changed

Lines changed: 234 additions & 74 deletions

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,45 @@ typedef NS_ENUM(NSInteger, SSJStaticCellType) {
455455
>为了方便比较,同组页面的定制和分组是一致的。我们可以看到,定制代码都作用于cell的viewModel上了。
456456
457457

458+
## 4. 刷新数据源
458459
----
460+
支持更新数据源后,刷新数据源。试着在发现页模拟网络请求,在请求结束后更新某个cell的viewmodel。举个例子:刷新一下发现页里最后一行的cell:
459461

460-
非常感谢你的阅读,觉得不好请多批评
462+
```objc
463+
//模拟网络请求
464+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
465+
466+
//请求成功x
467+
NSDictionary *responseDict = @{@"title_info":@"新游戏上架啦",
468+
@"title_icon":@"game_1",
469+
@"game_info":@"一起来玩斗地主呀!",
470+
@"game_icon":@"doudizhu"
471+
};
472+
//将要刷新cell的indexPath
473+
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:3];
474+
475+
//获取cell对应的viewModel
476+
SJStaticTableviewCellViewModel *viewModel = [self.dataSource tableView:self.tableView cellViewModelAtIndexPath:indexPath];
477+
478+
if (viewModel) {
479+
//更新viewModel
480+
viewModel.leftTitle = responseDict[@"title_info"];
481+
viewModel.leftImage = [UIImage imageNamed:responseDict[@"title_icon"]];
482+
viewModel.indicatorLeftImage = [UIImage imageNamed:responseDict[@"game_icon"]];
483+
viewModel.indicatorLeftTitle = responseDict[@"game_info"];
484+
485+
//刷新tableview
486+
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
487+
}
488+
});
489+
```
490+
491+
效果图:
492+
493+
![](http://oih3a9o4n.bkt.clouddn.com/sjstatictableview_7.png)
494+
495+
496+
## License
497+
498+
SJStaticTableView is released under the MIT license.
461499

SJStaticTableViewDemo/SJStaticTableViewDemo.xcodeproj/project.pbxproj

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,33 +149,33 @@
149149
CB67C3B31EA9DF4D00B6125B /* SJStaticTableViewComponent */ = {
150150
isa = PBXGroup;
151151
children = (
152+
CB67C3B61EA9DF4D00B6125B /* Resources */,
152153
CB67C3B41EA9DF4D00B6125B /* Factory.h */,
153154
CB67C3B51EA9DF4D00B6125B /* Factory.m */,
154-
CB67C3B61EA9DF4D00B6125B /* Resources */,
155155
CB67C3B81EA9DF4D00B6125B /* SJConst.h */,
156156
CB67C3B91EA9DF4D00B6125B /* SJStaticTableView.h */,
157157
CB67C3BA1EA9DF4D00B6125B /* SJStaticTableView.m */,
158-
CB67C3BB1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryDisclosureIndicator.h */,
159-
CB67C3BC1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryDisclosureIndicator.m */,
160-
CB67C3BD1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryNone.h */,
161-
CB67C3BE1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryNone.m */,
162-
CB67C3BF1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessorySwitch.h */,
163-
CB67C3C01EA9DF4D00B6125B /* SJStaticTableViewCell+AccessorySwitch.m */,
158+
CB67C3C51EA9DF4D00B6125B /* SJStaticTableViewCell.h */,
159+
CB67C3C61EA9DF4D00B6125B /* SJStaticTableViewCell.m */,
164160
CB67C3C11EA9DF4D00B6125B /* SJStaticTableViewCell+Logout.h */,
165161
CB67C3C21EA9DF4D00B6125B /* SJStaticTableViewCell+Logout.m */,
166162
CB67C3C31EA9DF4D00B6125B /* SJStaticTableViewCell+MeAvatar.h */,
167163
CB67C3C41EA9DF4D00B6125B /* SJStaticTableViewCell+MeAvatar.m */,
168-
CB67C3C51EA9DF4D00B6125B /* SJStaticTableViewCell.h */,
169-
CB67C3C61EA9DF4D00B6125B /* SJStaticTableViewCell.m */,
164+
CB67C3BD1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryNone.h */,
165+
CB67C3BE1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryNone.m */,
166+
CB67C3BF1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessorySwitch.h */,
167+
CB67C3C01EA9DF4D00B6125B /* SJStaticTableViewCell+AccessorySwitch.m */,
168+
CB67C3BB1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryDisclosureIndicator.h */,
169+
CB67C3BC1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryDisclosureIndicator.m */,
170170
CB67C3C71EA9DF4D00B6125B /* SJStaticTableviewCellViewModel.h */,
171171
CB67C3C81EA9DF4D00B6125B /* SJStaticTableviewCellViewModel.m */,
172-
CB67C3C91EA9DF4D00B6125B /* SJStaticTableViewController.h */,
173-
CB67C3CA1EA9DF4D00B6125B /* SJStaticTableViewController.m */,
172+
CB67C3CE1EA9DF4D00B6125B /* SJStaticTableviewSectionViewModel.h */,
173+
CB67C3CF1EA9DF4D00B6125B /* SJStaticTableviewSectionViewModel.m */,
174174
CB67C3CB1EA9DF4D00B6125B /* SJStaticTableViewDataSource.h */,
175175
CB67C3CC1EA9DF4D00B6125B /* SJStaticTableViewDataSource.m */,
176+
CB67C3C91EA9DF4D00B6125B /* SJStaticTableViewController.h */,
177+
CB67C3CA1EA9DF4D00B6125B /* SJStaticTableViewController.m */,
176178
CB67C3CD1EA9DF4D00B6125B /* SJStaticTableViewHeader.h */,
177-
CB67C3CE1EA9DF4D00B6125B /* SJStaticTableviewSectionViewModel.h */,
178-
CB67C3CF1EA9DF4D00B6125B /* SJStaticTableviewSectionViewModel.m */,
179179
);
180180
path = SJStaticTableViewComponent;
181181
sourceTree = "<group>";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
type = "1"
4+
version = "2.0">
5+
<Breakpoints>
6+
<BreakpointProxy
7+
BreakpointExtensionID = "Xcode.Breakpoint.ExceptionBreakpoint">
8+
<BreakpointContent
9+
shouldBeEnabled = "Yes"
10+
ignoreCount = "0"
11+
continueAfterRunningActions = "No"
12+
scope = "0"
13+
stopOnStyle = "0">
14+
</BreakpointContent>
15+
</BreakpointProxy>
16+
</Breakpoints>
17+
</Bucket>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"scale" : "1x"
6+
},
7+
{
8+
"idiom" : "universal",
9+
"filename" : "doudizhu@2x.png",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"version" : 1,
19+
"author" : "xcode"
20+
}
21+
}
122 KB
Loading
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"scale" : "1x"
6+
},
7+
{
8+
"idiom" : "universal",
9+
"filename" : "game_1@2x.png",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"version" : 1,
19+
"author" : "xcode"
20+
}
21+
}
231 KB
Loading

SJStaticTableViewDemo/SJStaticTableViewDemo/SJStaticTableViewComponent/SJStaticTableViewCell+AccessoryDisclosureIndicator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
#import "SJStaticTableViewCell.h"
1010

1111
@interface SJStaticTableViewCell (AccessoryDisclosureIndicator)
12+
1213
- (void)configureAccessoryDisclosureIndicatorCellWithViewModel:(SJStaticTableviewCellViewModel *)viewModel;
14+
1315
@end

SJStaticTableViewDemo/SJStaticTableViewDemo/SJStaticTableViewComponent/SJStaticTableViewCell+AccessoryDisclosureIndicator.m

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
#import "SJStaticTableViewCell+AccessoryDisclosureIndicator.h"
1010

1111
@implementation SJStaticTableViewCell (AccessoryDisclosureIndicator)
12+
1213
- (void)configureAccessoryDisclosureIndicatorCellWithViewModel:(SJStaticTableviewCellViewModel *)viewModel
1314
{
14-
15+
//左侧控件的配置
1516
[self layoutLeftPartSubViewsWithViewModel:viewModel];
1617

1718
//右侧存在箭头
@@ -20,12 +21,60 @@ - (void)configureAccessoryDisclosureIndicatorCellWithViewModel:(SJStaticTablevie
2021
//右侧存在箭头+文字
2122
if ( (viewModel.indicatorLeftTitle) && (viewModel.indicatorLeftLabelSize.width != 0)) {
2223
[self.contentView addSubview:self.indicatorLeftLabel];
24+
[self configureIndicatorLeftLabel];
2325
}
2426

2527
//右侧存在箭头+图片
2628
if ((self.viewModel.indicatorLeftImage) && (viewModel.indicatorLeftImageSize.height != 0)){
2729
[self.contentView addSubview:self.indicatorLeftImageView];
30+
[self configureIndicatorLeftImage];
2831
}
32+
}
33+
34+
//将右侧label的配置从懒加载里抽出来
35+
- (void)configureIndicatorLeftLabel
36+
{
37+
38+
self.indicatorLeftLabel.font = self.viewModel.indicatorLeftLabelTextFont;
39+
self.indicatorLeftLabel.textColor = self.viewModel.indicatorLeftLabelTextColor;
40+
self.indicatorLeftLabel.text = self.viewModel.indicatorLeftTitle;
2941

42+
if (!self.viewModel.hasIndicatorImageAndLabel) {
43+
//只存在右侧Label
44+
self.indicatorLeftLabel.frame = CGRectMake(self.indicatorArrow.frame.origin.x - SJLeftGap - self.viewModel.indicatorLeftLabelSize.width , (self.viewModel.cellHeight - self.viewModel.indicatorLeftLabelSize.height)/2, self.viewModel.indicatorLeftLabelSize.width, self.viewModel.indicatorLeftLabelSize.height);
45+
}else{
46+
//右侧Label和image同时存在
47+
if (self.viewModel.isImageFirst) {
48+
49+
self.indicatorLeftLabel.frame = CGRectMake(SJScreenWidth - SJLeftGap - self.indicatorArrow.bounds.size.width - self.viewModel.indicatorLeftImageAndLabelGap - self.viewModel.indicatorLeftImageSize.width - SJLeftGap - self.viewModel.indicatorLeftLabelSize.width, (self.viewModel.cellHeight - self.viewModel.indicatorLeftLabelSize.height)/2, self.viewModel.indicatorLeftLabelSize.width, self.viewModel.indicatorLeftLabelSize.height);
50+
51+
}else{
52+
53+
self.indicatorLeftLabel.frame = CGRectMake(SJScreenWidth - SJLeftGap - self.indicatorArrow.bounds.size.width - SJLeftGap - self.viewModel.indicatorLeftLabelSize.width, (self.viewModel.cellHeight - self.viewModel.indicatorLeftLabelSize.height)/2, self.viewModel.indicatorLeftLabelSize.width, self.viewModel.indicatorLeftLabelSize.height);
54+
}
55+
}
3056
}
57+
58+
//将右侧image的配置从懒加载里抽出来
59+
- (void)configureIndicatorLeftImage
60+
{
61+
self.indicatorLeftImageView.image = self.viewModel.indicatorLeftImage;
62+
63+
if (!self.viewModel.hasIndicatorImageAndLabel) {
64+
//只存在右侧image
65+
self.indicatorLeftImageView.frame = CGRectMake(self.indicatorArrow.frame.origin.x - SJLeftGap - self.viewModel.indicatorLeftImageSize.width, (self.viewModel.cellHeight - self.viewModel.indicatorLeftImageSize.height)/2, self.viewModel.indicatorLeftImageSize.width , self.viewModel.indicatorLeftImageSize.height);
66+
}else{
67+
//右侧Label和image同时存在
68+
if (self.viewModel.isImageFirst) {
69+
70+
self.indicatorLeftImageView.frame = CGRectMake(SJScreenWidth - SJLeftGap - self.indicatorArrow.bounds.size.width - SJLeftGap - self.viewModel.indicatorLeftImageSize.width, (self.viewModel.cellHeight - self.viewModel.indicatorLeftImageSize.height)/2, self.viewModel.indicatorLeftImageSize.width, self.viewModel.indicatorLeftImageSize.height);
71+
72+
}else{
73+
74+
self.indicatorLeftImageView.frame = CGRectMake(SJScreenWidth - SJLeftGap - self.indicatorArrow.bounds.size.width - self.viewModel.indicatorLeftImageAndLabelGap - self.viewModel.indicatorLeftLabelSize.width - SJLeftGap - self.viewModel.indicatorLeftImageSize.width, (self.viewModel.cellHeight - self.viewModel.indicatorLeftImageSize.height)/2, self.viewModel.indicatorLeftImageSize.width,self.viewModel.indicatorLeftImageSize.height);
75+
}
76+
77+
}
78+
}
79+
3180
@end

0 commit comments

Comments
 (0)