Last active
January 3, 2016 13:19
-
-
Save kormie/8468625 to your computer and use it in GitHub Desktop.
Rendering UITableViewCells
This file contains hidden or 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
| @interface Model : NSObject | |
| @property (nonatomic, copy) NSString *name; | |
| @end |
This file contains hidden or 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
| @interface ModelTableCell : UITableViewCell <ModelViewProtocol> | |
| @end |
This file contains hidden or 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
| - (void)displayName:(NSString *)name{ | |
| self.nameLabel.text = name | |
| } |
This file contains hidden or 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
| @protocol ModelViewProtocol <NSObject> | |
| @optional | |
| - (void)displayName:(NSString*)displayName; | |
| @end | |
| @interface ModelViewDelegate : NSObject | |
| - (instancetype)initWithModel:(Model *)model | |
| - (void)render:(id<ModelViewProtocol>)view | |
| @end |
This file contains hidden or 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
| @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 |
This file contains hidden or 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
| - (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