forked from wujunyang/MobileProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
331 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
MobileProject/Main/Theory/Controller/MPDataSourceViewController.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// MPDataSourceViewController.h | ||
// MobileProject 一个关于Table Data Source优化的提取 | ||
// | ||
// Created by wujunyang on 2017/3/31. | ||
// Copyright © 2017年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface MPDataSourceViewController : UIViewController | ||
|
||
@end |
82 changes: 82 additions & 0 deletions
82
MobileProject/Main/Theory/Controller/MPDataSourceViewController.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// MPDataSourceViewController.m | ||
// MobileProject | ||
// | ||
// Created by wujunyang on 2017/3/31. | ||
// Copyright © 2017年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import "MPDataSourceViewController.h" | ||
#import "MPArrayDataSource.h" | ||
#import "MPPhotoCell.h" | ||
#import "MPPhotoCell+ConfigureForPhoto.h" | ||
|
||
|
||
@interface MPDataSourceViewController()<UITableViewDelegate> | ||
|
||
@property (nonatomic,strong) NSArray *dataArray; | ||
@property (nonatomic,strong) UITableView *myTableView; | ||
@property(nonatomic,strong)MPArrayDataSource *photosArrayDataSource; | ||
@end | ||
|
||
|
||
@implementation MPDataSourceViewController | ||
|
||
#pragma mark 生命周期 | ||
|
||
- (void)viewDidLoad { | ||
[super viewDidLoad]; | ||
self.view.backgroundColor=[UIColor whiteColor]; | ||
self.navigationItem.title=@"TableViewDataSource提取"; | ||
|
||
if (!self.dataArray) { | ||
self.dataArray=@[@"照片一",@"照片二",@"照片三",@"照片四",@"照片五"]; | ||
} | ||
|
||
//初始化表格 | ||
if (!_myTableView) { | ||
_myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0.5, Main_Screen_Width, Main_Screen_Height) style:UITableViewStylePlain]; | ||
_myTableView.showsVerticalScrollIndicator = NO; | ||
_myTableView.showsHorizontalScrollIndicator = NO; | ||
_myTableView.delegate = self; | ||
[self.view addSubview:_myTableView]; | ||
[_myTableView mas_makeConstraints:^(MASConstraintMaker *make) { | ||
make.edges.equalTo(UIEdgeInsetsMake(0, 0, 0, 0)); | ||
}]; | ||
} | ||
|
||
[self setupTableDataSource]; | ||
} | ||
|
||
- (void)didReceiveMemoryWarning { | ||
[super didReceiveMemoryWarning]; | ||
} | ||
|
||
#pragma mark 自定义代码 | ||
|
||
- (void)setupTableDataSource | ||
{ | ||
TableViewCellConfigureBlock configureCell = ^(MPPhotoCell *cell, NSString *photoName) { | ||
[cell configureForPhoto:photoName]; | ||
}; | ||
self.photosArrayDataSource = [[MPArrayDataSource alloc] initWithItems:self.dataArray | ||
cellIdentifier:NSStringFromClass([MPPhotoCell class]) | ||
configureCellBlock:configureCell]; | ||
self.myTableView.dataSource = self.photosArrayDataSource; | ||
[self.myTableView registerClass:[MPPhotoCell class] forCellReuseIdentifier:NSStringFromClass([MPPhotoCell class])]; | ||
} | ||
|
||
|
||
#pragma mark UITableViewDelegate内容 | ||
|
||
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ | ||
return 44; | ||
} | ||
|
||
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ | ||
[tableView deselectRowAtIndexPath:indexPath animated:YES]; | ||
|
||
NSLog(@"当前的名称:%@",self.dataArray[indexPath.row]); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
MobileProject/Main/Theory/Other/DataSource/MPArrayDataSource.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// MPArrayDataSource.h | ||
// MobileProject | ||
// | ||
// Created by wujunyang on 2017/3/31. | ||
// Copyright © 2017年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
typedef void (^TableViewCellConfigureBlock)(id cell, id item); | ||
|
||
@interface MPArrayDataSource : NSObject <UITableViewDataSource> | ||
|
||
- (id)initWithItems:(NSArray *)anItems | ||
cellIdentifier:(NSString *)aCellIdentifier | ||
configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock; | ||
|
||
- (id)itemAtIndexPath:(NSIndexPath *)indexPath; | ||
|
||
@end |
57 changes: 57 additions & 0 deletions
57
MobileProject/Main/Theory/Other/DataSource/MPArrayDataSource.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// MPArrayDataSource.m | ||
// MobileProject | ||
// | ||
// Created by wujunyang on 2017/3/31. | ||
// Copyright © 2017年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import "MPArrayDataSource.h" | ||
|
||
@interface MPArrayDataSource () | ||
|
||
@property (nonatomic, strong) NSArray *items; | ||
@property (nonatomic, copy) NSString *cellIdentifier; | ||
@property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock; | ||
|
||
@end | ||
|
||
|
||
@implementation MPArrayDataSource | ||
|
||
- (id)initWithItems:(NSArray *)anItems | ||
cellIdentifier:(NSString *)aCellIdentifier | ||
configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock | ||
{ | ||
self = [super init]; | ||
if (self) { | ||
self.items = anItems; | ||
self.cellIdentifier = aCellIdentifier; | ||
self.configureCellBlock = [aConfigureCellBlock copy]; | ||
} | ||
return self; | ||
} | ||
|
||
- (id)itemAtIndexPath:(NSIndexPath *)indexPath | ||
{ | ||
return self.items[(NSUInteger) indexPath.row]; | ||
} | ||
|
||
|
||
#pragma mark UITableViewDataSource | ||
|
||
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | ||
{ | ||
return self.items.count; | ||
} | ||
|
||
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | ||
{ | ||
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier | ||
forIndexPath:indexPath]; | ||
id item = [self itemAtIndexPath:indexPath]; | ||
self.configureCellBlock(cell, item); | ||
return cell; | ||
} | ||
|
||
@end |
15 changes: 15 additions & 0 deletions
15
MobileProject/Main/Theory/View/MPPhotoCell+ConfigureForPhoto.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// MPPhotoCell+ConfigureForPhoto.h | ||
// MobileProject | ||
// | ||
// Created by wujunyang on 2017/3/31. | ||
// Copyright © 2017年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import "MPPhotoCell.h" | ||
|
||
@interface MPPhotoCell (ConfigureForPhoto) | ||
|
||
- (void)configureForPhoto:(NSString *)photoName; | ||
|
||
@end |
18 changes: 18 additions & 0 deletions
18
MobileProject/Main/Theory/View/MPPhotoCell+ConfigureForPhoto.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// MPPhotoCell+ConfigureForPhoto.m | ||
// MobileProject | ||
// | ||
// Created by wujunyang on 2017/3/31. | ||
// Copyright © 2017年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import "MPPhotoCell+ConfigureForPhoto.h" | ||
|
||
@implementation MPPhotoCell (ConfigureForPhoto) | ||
|
||
- (void)configureForPhoto:(NSString *)photoName | ||
{ | ||
self.myNameLabel.text=photoName; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// MPPhotoCell.h | ||
// MobileProject | ||
// | ||
// Created by wujunyang on 2017/3/31. | ||
// Copyright © 2017年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@interface MPPhotoCell : UITableViewCell | ||
|
||
@property(nonatomic,strong,readonly)UILabel *myNameLabel; | ||
|
||
@end |
Oops, something went wrong.