, , - , , , , , UITableViewController :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *aCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
if ([aCell.reuseIdentifier isEqualToString:@"someIdentifier"]){
}
else if ([aCell.reuseIdentifier isEqualToString:@"someOtherIdentifier"]) {
}
return aCell;
}
;
1) .m typedef:
typedef void(^IDPCellConfigurationBlock)(UITableViewCell *aCell);
2) cellConfigurations TablviewControllerSubclass:
@interface IPDSettingsTableViewController ()
@property (nonatomic, strong) NSDictionary *cellConfigurations;
@property (nonatomic) id dataModel;
@end
3) TableviewController xib
cellReuseIdentifier ,
4) viewDidLoad :
- (void)viewDidLoad
{
[super viewDidLoad];
[self SetupCellsConfigurationBlocks];
}
- (void)SetupCellsConfigurationBlocks
{
NSMutableDictionary *cellsConfigurationBlocks = [NSMutableDictionary new];
cellsConfigurationBlocks[@"someCellIdentifier"] = ^(UITableViewCell *aCell){
aCell.backgroundColor = [UIColor orangeColor];
};
cellsConfigurationBlocks[@"otherCellIdentifier"] = ^(UITableViewCell *aCell){
aCell.imageView.image = [UIImage imageNamed:@"some image name"];
};
__weak typeof (self) weakSelf = self;
cellsConfigurationBlocks[@"nextCellIdentifier"] = ^(UITableViewCell *aCell){
aCell.textLabel.textColor = [[weakSelf.dataModel someProperty] isEqual:@YES] ? [UIColor purpleColor] : [UIColor yellowColor];
aCell.textLabel.text = [weakSelf.dataModel someOtherProperty];
};
weakSelf.cellConfigurations = [cellsConfigurationBlocks copy];
}
5) overload tableView: cellForRowAtIndexPath :
#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *aCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
[self configureCell:aCell withConfigurationBlock:self.cellConfigurations[aCell.reuseIdentifier]];
return aCell;
}
- (void)configureCell:(UITableViewCell *)aCell withConfigurationBlock:(IDPCellConfigurationBlock)configureCellBlock
{
if (configureCellBlock){
configureCellBlock(aCell);
}
}