iOS將數據從controller里分離出來減輕controller的壓力

hifr1816 7年前發布 | 8K 次閱讀 UIKit iOS開發 移動開發

今天要說的是一種代碼優化的技巧,我們知道,編程一直講求高聚合,低耦合。一般地,我們會將一個頁面的 UITableviewDelegate 和 UITableviewDataSource 寫在同一個controller里,如果你只有一個數據源,這樣寫是可以的。但假設下面這種情況,如圖,一個頁面有多個數據源,如果在同一個controller里寫的話,一個controller同時要遵守 UITableViewDelegate,UITableviewDataSource,UICollectionDelegate,UICollectionDataSource, 然后將這些代理實現,那controller的壓力是不是會變得很大呢?接下來就是減壓的時刻了。

兩個數據源.png

先看下項目中的效果圖。

分離出數據.jpeg

controller中的代理實現.jpeg

下面以圖中左邊的tableview為例。

先建一個類繼承自 NSObject ,遵循 UITableviewDataSource ,并定義一個block,寫兩個對象方法,定義一個數組,代碼如下:

XSMutiCatagoryTableviewDataSource.h

#import <UIKit/UIKit.h>

typedef void (^MutiCatagoryTableViewCellConfigureBlock)(id cell, id item);

@interface XSMutiCatagoryTableViewDataSource : NSObject <UITableViewDataSource>

@property (nonatomic, copy) NSArray *items;

- (id)initWithItems:(NSArray *)items cellIdentifier:(NSString *)cellIdentifier configureCellBlock:(MutiCatagoryTableViewCellConfigureBlock)configureCellBlock;

- (id)itemAtIndexPath:(NSIndexPath *)indexPath;

@end

XSMutiCatagoryTableviewDataSource.m

#import "XSMutiCatagoryTableViewDataSource.h"

@interface XSMutiCatagoryTableViewDataSource ()

@property (nonatomic, copy) NSString *cellIdentifier;
@property (nonatomic, copy) MutiCatagoryTableViewCellConfigureBlock configureCellBlock;
@end

@implementation XSMutiCatagoryTableViewDataSource

- (id)initWithItems:(NSArray *)items cellIdentifier:(NSString *)cellIdentifier configureCellBlock:(MutiCatagoryTableViewCellConfigureBlock)configureCellBlock {
    self = [super init];
    if (self) {
        self.items = items;
        self.cellIdentifier = cellIdentifier;
        self.configureCellBlock = configureCellBlock;
    }
    return self;
}

#pragma mark - public methods
- (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

然后就是controller里的事情了

XSMutiCatagoryViewController.m

#import "XSMutiCatagoryTableViewDataSource.h"
@interface XSMutiCatagoryViewController () <UITableViewDelegate>
@property (strong, nonatomic) XSMutiCatagoryTableViewDataSource *mutiCatagoryTableViewDataSource;
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) MenuBridge *menuBridge;//數據源
@end
@implementation XSMutiCatagoryViewController

#pragma mark - life cycle
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    CGFloat width  = [UIScreen mainScreen].bounds.size.width;
    self.tableView.frame      = CGRectMake(0,64, width / 4, height-49-64);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self loadMenuData];
}
#pragma mark - http menthods
- (void)loadMenuData {
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    XSAPIManager *manager = [XSAPIManager manager];
    [manager GET:url parameters:parameters success:^(id responseObject) {
        //NSLog(@"分類頁tableview數據%@",responseObject);
        weakSelf.menuBridge = [MenuBridge mj_objectWithKeyValues:responseObject];

        if (weakSelf.menuBridge.data.count > 0) {
            weakSelf.mutiCatagoryTableViewDataSource.items = weakSelf.menuBridge.data;
            [weakSelf.tableView reloadData];
            [weakSelf.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionBottom];
            [weakSelf tableView:weakSelf.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        }

        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
    } failure:^(NSError *error) {

        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
    }];
}
#pragma mark - getters and setters
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] init];
        _tableView.backgroundColor = MENU_COLOR;
        _tableView.separatorColor  = OTHER_SEPARATOR_COLOR;
        [_tableView registerNib:[UINib nibWithNibName:@"XSMutiCatagoryTableViewCell" bundle:nil] forCellReuseIdentifier:tableCellId];
        _tableView.delegate   = self;
        _tableView.dataSource = self.mutiCatagoryTableViewDataSource;
        _tableView.rowHeight = 49.0f;
    }
    return _tableView;
}

- (XSMutiCatagoryTableViewDataSource *)mutiCatagoryTableViewDataSource {
    if (_mutiCatagoryTableViewDataSource == nil) {
        _mutiCatagoryTableViewDataSource = [[XSMutiCatagoryTableViewDataSource alloc] initWithItems:self.menuBridge.data cellIdentifier:tableCellId configureCellBlock:^(XSMutiCatagoryTableViewCell *cell, Menu *item) {
            [cell configureForMenuItem:item];
        }];
    }
    return _mutiCatagoryTableViewDataSource;
}

以上。

 

來自:http://www.jianshu.com/p/9bb0ff4d2e02

 

 本文由用戶 hifr1816 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!