分離tableView的DataSource

MVC下分離tableView的Datasource
封裝一個繼承與NSObject的ArrayDataSource類來專門處理tableView的數(shù)據(jù)

代碼如下:
ArrayDataSource.h中

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

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


@interface ArrayDataSource : NSObject <UITableViewDataSource>

- (id)initWithItems:(NSArray *)anItems
     cellIdentifier:(NSString *)aCellIdentifier
 configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;

//用于取出當(dāng)前Cell的model 
- (id)itemAtIndexPath:(NSIndexPath *)indexPath;

@end

ArrayDataSource.m中

#import "ArrayDataSource.h"


@interface ArrayDataSource ()

@property (nonatomic, strong) NSArray *items;
@property (nonatomic, copy) NSString *cellIdentifier;
@property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock;

@end


@implementation ArrayDataSource

- (id)init
{
    return nil;
}

- (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

實現(xiàn)代碼:

static NSString * const PhotoCellIdentifier = @"PhotoCell";

- (void)setupTableView
{
    TableViewCellConfigureBlock configureCell = ^(UITableViewCell *cell, NSString *str) {
        
        cell.textLabel.text = str;
    };
// Demo 中簡單起見傳的字符串, 一般是傳Model的
    NSArray *photos = @[@"jiang",@"yong",@"chang"];
    self.photosArrayDataSource = [[ArrayDataSource alloc] initWithItems:photos
                                                         cellIdentifier:PhotoCellIdentifier
                                                     configureCellBlock:configureCell];
    self.dataSource = self.photosArrayDataSource;
    
    [self registerClass:[UITableViewCell class] forCellReuseIdentifier:PhotoCellIdentifier];
//    [self.tableView registerNib:[PhotoCell nib] forCellReuseIdentifier:PhotoCellIdentifier];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *pasStr = [self.photosArrayDataSource itemAtIndexPath:indexPath];
    
    if ([self.myDelegate respondsToSelector:@selector(passStrToController:)]) {
        
        [self.myDelegate passStrToController:pasStr];
    }
}

但是有一個問題###

   Demo中把tableView和Controller分離了開來,導(dǎo)致處理數(shù)據(jù)
   NSArray *photos = @[@"jiang",@"yong",@"chang"];
   只能在tableView中賦值, 

   而不能從controller中傳遞過來
  // NSArray *photos = self.dataArray;

  原因:在初始化的時候已經(jīng)給NSArray *photos賦值了, 而當(dāng)時還沒有傳值,所以為空.

所以最好在UITableViewController中使用####

Demo下載地址

參考文章(英文)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容