코드를 더 재사용 할 수 있도록 UITableview의 데이터 소스를 분리하는 방법에 대한 온라인 예제를 발견했습니다. 그러나이 코드에서는 델리게이트 대신 블록을 사용하여 셀을 업데이트합니다. 지금까지는 프로젝트에서 대리인을 사용하여 일관성을 유지하면서이 방법을 그대로 유지하여 누군가가 더 나은 블록을 설명하고 아래 예제 코드를 리팩터링하여 블록 대신 delegates
을 포함시킬 수있게 도와 주시면 감사하겠습니다. 사용중인 예제는 http://www.objc.io/issue-1/lighter-view-controllers.html입니다.델리게이트 대 블록 이해. (블록 대신 위임을 사용하는 리팩토링 코드)
//
// ArrayDataSource.h
// objc.io example project (issue #1)
//
#import <Foundation/Foundation.h>
typedef void (^TableViewCellConfigureBlock)(id cell, id item);
@interface ArrayDataSource : NSObject <UITableViewDataSource>
- (id)initWithItems:(NSArray *)anItems
cellIdentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;
- (id)itemAtIndexPath:(NSIndexPath *)indexPath;
@end
하는 .m
//
// ArrayDataSource.h
// objc.io example project (issue #1)
//
#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
그리고 UIViewController
에서의 사용은 될 것이다;
void (^configureCell)(PhotoCell*, Photo*) = ^(PhotoCell* cell, Photo* photo) {
cell.label.text = photo.name;
};
photosArrayDataSource = [[ArrayDataSource alloc] initWithItems:photos
cellIdentifier:PhotoCellIdentifier
configureCellBlock:configureCell];
self.tableView.dataSource = photosArrayDataSource;
나는 매일 새로운 것을 배우려고 노력하며 누군가 도움을 주면 좋을 것입니다. 언젠가 한 번에 개발자로서 개선하기를 희망합니다 :
데이터 소스의 분리를 이해하지만 위임자 대신 블록을 사용하는 것이 나에게 새롭습니다.
대단히 감사합니다. 이것은 매우 도움이됩니다. :) – DevC