Skip to content

Commit

Permalink
支持刷新数据源更新cell的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
knightsj committed Apr 22, 2017
1 parent bfd7899 commit ec0ba98
Show file tree
Hide file tree
Showing 21 changed files with 234 additions and 74 deletions.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,45 @@ typedef NS_ENUM(NSInteger, SSJStaticCellType) {
>为了方便比较,同组页面的定制和分组是一致的。我们可以看到,定制代码都作用于cell的viewModel上了。

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

非常感谢你的阅读,觉得不好请多批评
```objc
//模拟网络请求
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

//请求成功x
NSDictionary *responseDict = @{@"title_info":@"新游戏上架啦",
@"title_icon":@"game_1",
@"game_info":@"一起来玩斗地主呀!",
@"game_icon":@"doudizhu"
};
//将要刷新cell的indexPath
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:3];

//获取cell对应的viewModel
SJStaticTableviewCellViewModel *viewModel = [self.dataSource tableView:self.tableView cellViewModelAtIndexPath:indexPath];

if (viewModel) {
//更新viewModel
viewModel.leftTitle = responseDict[@"title_info"];
viewModel.leftImage = [UIImage imageNamed:responseDict[@"title_icon"]];
viewModel.indicatorLeftImage = [UIImage imageNamed:responseDict[@"game_icon"]];
viewModel.indicatorLeftTitle = responseDict[@"game_info"];

//刷新tableview
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
});
```
效果图:
![](http://oih3a9o4n.bkt.clouddn.com/sjstatictableview_7.png)
## License
SJStaticTableView is released under the MIT license.
Original file line number Diff line number Diff line change
Expand Up @@ -149,33 +149,33 @@
CB67C3B31EA9DF4D00B6125B /* SJStaticTableViewComponent */ = {
isa = PBXGroup;
children = (
CB67C3B61EA9DF4D00B6125B /* Resources */,
CB67C3B41EA9DF4D00B6125B /* Factory.h */,
CB67C3B51EA9DF4D00B6125B /* Factory.m */,
CB67C3B61EA9DF4D00B6125B /* Resources */,
CB67C3B81EA9DF4D00B6125B /* SJConst.h */,
CB67C3B91EA9DF4D00B6125B /* SJStaticTableView.h */,
CB67C3BA1EA9DF4D00B6125B /* SJStaticTableView.m */,
CB67C3BB1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryDisclosureIndicator.h */,
CB67C3BC1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryDisclosureIndicator.m */,
CB67C3BD1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryNone.h */,
CB67C3BE1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryNone.m */,
CB67C3BF1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessorySwitch.h */,
CB67C3C01EA9DF4D00B6125B /* SJStaticTableViewCell+AccessorySwitch.m */,
CB67C3C51EA9DF4D00B6125B /* SJStaticTableViewCell.h */,
CB67C3C61EA9DF4D00B6125B /* SJStaticTableViewCell.m */,
CB67C3C11EA9DF4D00B6125B /* SJStaticTableViewCell+Logout.h */,
CB67C3C21EA9DF4D00B6125B /* SJStaticTableViewCell+Logout.m */,
CB67C3C31EA9DF4D00B6125B /* SJStaticTableViewCell+MeAvatar.h */,
CB67C3C41EA9DF4D00B6125B /* SJStaticTableViewCell+MeAvatar.m */,
CB67C3C51EA9DF4D00B6125B /* SJStaticTableViewCell.h */,
CB67C3C61EA9DF4D00B6125B /* SJStaticTableViewCell.m */,
CB67C3BD1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryNone.h */,
CB67C3BE1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryNone.m */,
CB67C3BF1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessorySwitch.h */,
CB67C3C01EA9DF4D00B6125B /* SJStaticTableViewCell+AccessorySwitch.m */,
CB67C3BB1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryDisclosureIndicator.h */,
CB67C3BC1EA9DF4D00B6125B /* SJStaticTableViewCell+AccessoryDisclosureIndicator.m */,
CB67C3C71EA9DF4D00B6125B /* SJStaticTableviewCellViewModel.h */,
CB67C3C81EA9DF4D00B6125B /* SJStaticTableviewCellViewModel.m */,
CB67C3C91EA9DF4D00B6125B /* SJStaticTableViewController.h */,
CB67C3CA1EA9DF4D00B6125B /* SJStaticTableViewController.m */,
CB67C3CE1EA9DF4D00B6125B /* SJStaticTableviewSectionViewModel.h */,
CB67C3CF1EA9DF4D00B6125B /* SJStaticTableviewSectionViewModel.m */,
CB67C3CB1EA9DF4D00B6125B /* SJStaticTableViewDataSource.h */,
CB67C3CC1EA9DF4D00B6125B /* SJStaticTableViewDataSource.m */,
CB67C3C91EA9DF4D00B6125B /* SJStaticTableViewController.h */,
CB67C3CA1EA9DF4D00B6125B /* SJStaticTableViewController.m */,
CB67C3CD1EA9DF4D00B6125B /* SJStaticTableViewHeader.h */,
CB67C3CE1EA9DF4D00B6125B /* SJStaticTableviewSectionViewModel.h */,
CB67C3CF1EA9DF4D00B6125B /* SJStaticTableviewSectionViewModel.m */,
);
path = SJStaticTableViewComponent;
sourceTree = "<group>";
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.ExceptionBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
scope = "0"
stopOnStyle = "0">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
#import "SJStaticTableViewCell.h"

@interface SJStaticTableViewCell (AccessoryDisclosureIndicator)

- (void)configureAccessoryDisclosureIndicatorCellWithViewModel:(SJStaticTableviewCellViewModel *)viewModel;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#import "SJStaticTableViewCell+AccessoryDisclosureIndicator.h"

@implementation SJStaticTableViewCell (AccessoryDisclosureIndicator)

- (void)configureAccessoryDisclosureIndicatorCellWithViewModel:(SJStaticTableviewCellViewModel *)viewModel
{

//左侧控件的配置
[self layoutLeftPartSubViewsWithViewModel:viewModel];

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

//右侧存在箭头+图片
if ((self.viewModel.indicatorLeftImage) && (viewModel.indicatorLeftImageSize.height != 0)){
[self.contentView addSubview:self.indicatorLeftImageView];
[self configureIndicatorLeftImage];
}
}

//将右侧label的配置从懒加载里抽出来
- (void)configureIndicatorLeftLabel
{

self.indicatorLeftLabel.font = self.viewModel.indicatorLeftLabelTextFont;
self.indicatorLeftLabel.textColor = self.viewModel.indicatorLeftLabelTextColor;
self.indicatorLeftLabel.text = self.viewModel.indicatorLeftTitle;

if (!self.viewModel.hasIndicatorImageAndLabel) {
//只存在右侧Label
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);
}else{
//右侧Label和image同时存在
if (self.viewModel.isImageFirst) {

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);

}else{

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);
}
}
}

//将右侧image的配置从懒加载里抽出来
- (void)configureIndicatorLeftImage
{
self.indicatorLeftImageView.image = self.viewModel.indicatorLeftImage;

if (!self.viewModel.hasIndicatorImageAndLabel) {
//只存在右侧image
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);
}else{
//右侧Label和image同时存在
if (self.viewModel.isImageFirst) {

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);

}else{

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);
}

}
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@
//统一的,布局cell左侧部分的内容(标题 / 图片 + 标题),所有系统风格的cell都要调用这个方法
- (void)layoutLeftPartSubViewsWithViewModel:(SJStaticTableviewCellViewModel *)viewModel;


@end
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,37 @@ - (void)layoutLeftPartSubViewsWithViewModel:(SJStaticTableviewCellViewModel *)vi

//存在最左边的图片
if (self.viewModel.leftImage) {

[self.contentView addSubview:self.leftImageView];

self.leftImageView.image = self.viewModel.leftImage;
self.leftImageView.frame = CGRectMake(SJLeftGap, (self.viewModel.cellHeight - self.viewModel.leftImageSize.height)/2, self.viewModel.leftImageSize.width , self.viewModel.leftImageSize.height);
}


//存在标题文字
if (self.viewModel.leftTitle.length) {

[self.contentView addSubview:self.leftTitleLabel];

self.leftTitleLabel.font = self.viewModel.leftLabelTextFont;
self.leftTitleLabel.textColor = self.viewModel.leftLabelTextColor;
self.leftTitleLabel.text = self.viewModel.leftTitle;

CGFloat x = 0;
if (_leftImageView) {
x = CGRectGetMaxX(self.leftImageView.frame) + self.viewModel.leftImageAndLabelGap;
}else{
x = SJLeftGap;
}

self.leftTitleLabel.frame = CGRectMake( x, (self.viewModel.cellHeight - self.viewModel.leftTitleLabelSize.height)/2, self.viewModel.leftTitleLabelSize.width, self.viewModel.leftTitleLabelSize.height);
}

}



#pragma mark- switch block
- (void)switchTouched:(UISwitch *)indicatorSwitch
{
Expand All @@ -41,8 +62,7 @@ - (void)switchTouched:(UISwitch *)indicatorSwitch
- (UIImageView *)leftImageView
{
if (!_leftImageView) {
_leftImageView = [[UIImageView alloc] initWithImage:_viewModel.leftImage];
_leftImageView.frame = CGRectMake(SJLeftGap, (_viewModel.cellHeight - _viewModel.leftImageSize.height)/2, _viewModel.leftImageSize.width , _viewModel.leftImageSize.height);
_leftImageView = [[UIImageView alloc] init];
}
return _leftImageView;
}
Expand All @@ -51,16 +71,6 @@ - (UILabel *)leftTitleLabel
{
if (!_leftTitleLabel) {
_leftTitleLabel= [[UILabel alloc] init];
_leftTitleLabel.font = _viewModel.leftLabelTextFont;
_leftTitleLabel.textColor = _viewModel.leftLabelTextColor;
_leftTitleLabel.text = _viewModel.leftTitle;
CGFloat x = 0;
if (_leftImageView) {
x = CGRectGetMaxX(_leftImageView.frame) + _viewModel.leftImageAndLabelGap;
}else{
x = SJLeftGap;
}
_leftTitleLabel.frame = CGRectMake( x, (_viewModel.cellHeight - _viewModel.leftTitleLabelSize.height)/2, _viewModel.leftTitleLabelSize.width, _viewModel.leftTitleLabelSize.height);
}
return _leftTitleLabel;
}
Expand All @@ -82,58 +92,22 @@ - (UIImageView *)indicatorArrow
if (!_indicatorArrow) {
_indicatorArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:SJIndicatorArrow]];
_indicatorArrow.frame = CGRectMake(SJScreenWidth - SJLeftGap - _indicatorArrow.bounds.size.width, (_viewModel.cellHeight - _indicatorArrow.bounds.size.height)/2, _indicatorArrow.bounds.size.width, _indicatorArrow.bounds.size.height);
}

}
return _indicatorArrow;
}

- (UILabel *)indicatorLeftLabel
{
if (!_indicatorLeftLabel) {
_indicatorLeftLabel= [[UILabel alloc] init];
_indicatorLeftLabel.font = _viewModel.indicatorLeftLabelTextFont;
_indicatorLeftLabel.textColor = _viewModel.indicatorLeftLabelTextColor;
_indicatorLeftLabel.text = _viewModel.indicatorLeftTitle;

if (!_viewModel.hasIndicatorImageAndLabel) {
_indicatorLeftLabel.frame = CGRectMake(_indicatorArrow.frame.origin.x - SJLeftGap - _viewModel.indicatorLeftLabelSize.width , (_viewModel.cellHeight - _viewModel.indicatorLeftLabelSize.height)/2, _viewModel.indicatorLeftLabelSize.width, _viewModel.indicatorLeftLabelSize.height);
}else{
//同时存在
if (_viewModel.isImageFirst) {

_indicatorLeftLabel.frame = CGRectMake(SJScreenWidth - SJLeftGap - _indicatorArrow.bounds.size.width - _viewModel.indicatorLeftImageAndLabelGap - _viewModel.indicatorLeftImageSize.width - SJLeftGap - _viewModel.indicatorLeftLabelSize.width, (_viewModel.cellHeight - _viewModel.indicatorLeftLabelSize.height)/2, _viewModel.indicatorLeftLabelSize.width, _viewModel.indicatorLeftLabelSize.height);

}else{

_indicatorLeftLabel.frame = CGRectMake(SJScreenWidth - SJLeftGap - _indicatorArrow.bounds.size.width - SJLeftGap - _viewModel.indicatorLeftLabelSize.width, (_viewModel.cellHeight - _viewModel.indicatorLeftLabelSize.height)/2, _viewModel.indicatorLeftLabelSize.width, _viewModel.indicatorLeftLabelSize.height);
}
}

_indicatorLeftLabel= [[UILabel alloc] init];
}
return _indicatorLeftLabel;
}

- (UIImageView *)indicatorLeftImageView
{
if (!_indicatorLeftImageView) {
_indicatorLeftImageView = [[UIImageView alloc] initWithImage:_viewModel.indicatorLeftImage];

if (!_viewModel.hasIndicatorImageAndLabel) {
_indicatorLeftImageView.frame = CGRectMake(_indicatorArrow.frame.origin.x - SJLeftGap - _viewModel.indicatorLeftImageSize.width, (_viewModel.cellHeight - _viewModel.indicatorLeftImageSize.height)/2, _viewModel.indicatorLeftImageSize.width , _viewModel.indicatorLeftImageSize.height);
}else{

if (_viewModel.isImageFirst) {

_indicatorLeftImageView.frame = CGRectMake(SJScreenWidth - SJLeftGap - _indicatorArrow.bounds.size.width - SJLeftGap - _viewModel.indicatorLeftImageSize.width, (_viewModel.cellHeight - _viewModel.indicatorLeftImageSize.height)/2, _viewModel.indicatorLeftImageSize.width, _viewModel.indicatorLeftImageSize.height);

}else{

_indicatorLeftImageView.frame = CGRectMake(SJScreenWidth - SJLeftGap - _indicatorArrow.bounds.size.width - _viewModel.indicatorLeftImageAndLabelGap - _viewModel.indicatorLeftLabelSize.width - SJLeftGap - _viewModel.indicatorLeftImageSize.width, (_viewModel.cellHeight - _viewModel.indicatorLeftImageSize.height)/2, _viewModel.indicatorLeftImageSize.width,_viewModel.indicatorLeftImageSize.height);

}

}

_indicatorLeftImageView = [[UIImageView alloc] init];
}
return _indicatorLeftImageView;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "SJStaticTableViewHeader.h"
#import "Factory.h"//假数据


@protocol SJStaticTableViewControllerDelegate <NSObject>

@required
Expand All @@ -21,4 +22,7 @@
@property (nonatomic, strong) SJStaticTableView *tableView;
@property (nonatomic, strong) SJStaticTableViewDataSource *dataSource;


- (SJStaticTableviewCellViewModel *)retrieveCellViewModelAtIndexPath:(NSIndexPath *)indexPath;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
#import "SJStaticTableViewController.h"
#import "SJStaticTableViewDataSource.h"

@interface SJStaticTableViewController()


@end


@implementation SJStaticTableViewController

Expand Down
Loading

0 comments on commit ec0ba98

Please sign in to comment.