Skip to content

Instantly share code, notes, and snippets.

@kormie
Last active January 3, 2016 13:19
Show Gist options
  • Select an option

  • Save kormie/8468625 to your computer and use it in GitHub Desktop.

Select an option

Save kormie/8468625 to your computer and use it in GitHub Desktop.
Rendering UITableViewCells
@interface Model : NSObject
@property (nonatomic, copy) NSString *name;
@end
@interface ModelTableCell : UITableViewCell <ModelViewProtocol>
@end
- (void)displayName:(NSString *)name{
self.nameLabel.text = name
}
@protocol ModelViewProtocol <NSObject>
@optional
- (void)displayName:(NSString*)displayName;
@end
@interface ModelViewDelegate : NSObject
- (instancetype)initWithModel:(Model *)model
- (void)render:(id<ModelViewProtocol>)view
@end
@interface ModelViewDelegate ()
@property (nonatomic, strong) Model *model;
@end
@implementation ModelViewDelegate
- (instancetype)initWithModel:(Model *)model{
self = [super init];
if (self){
self.model = model;
}
return self
}
- (void)render:(id<ModelViewProtocol>)view{
if ([view respondsToSelector:@selector(displayName:)]){
[view displayName:self.model.name];
}
}
@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ModelTableCell *cell = [tableView dequeueReusableCellWithIdentifier:[ModelTableCell identifier]];
if (cell == nil) {
cell = [ModelTableCell new];
}
Model *rowItem = self.tableData[indexPath.row];
ModelViewDelegate *viewDelegate = [ModelViewDelegate alloc] initWithModel:rowItem];
[viewDelegate render:cell];
return cell;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment