Refactors form field cell creation into separate reusable methods.
This commit is contained in:
@@ -38,24 +38,51 @@ const int kSectionIndexAbilityScores = 1;
|
|||||||
self.editingMonster = [[Monster alloc] initWithMonster:self.originalMonster];
|
self.editingMonster = [[Monster alloc] initWithMonster:self.originalMonster];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (MCShortStringFieldTableViewCell*) makeShortStringCellFromCell:(UITableViewCell*)cell {
|
- (UITableViewCell*) makeSafeCell {
|
||||||
if (cell == nil || ![cell isKindOfClass:[MCShortStringFieldTableViewCell class]]) {
|
#if DEBUG
|
||||||
// TODO: Figure out how to make this cell generate child views.
|
return nil;
|
||||||
return [[MCShortStringFieldTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MCShortStringField"];
|
#else
|
||||||
} else {
|
return [[UITableViewCell alloc] init];
|
||||||
return (MCShortStringFieldTableViewCell*)cell;
|
#endif
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (MCIntegerFieldTableViewCell*) makeIntegerCellFromCell:(UITableViewCell*)cell {
|
- (MCShortStringFieldTableViewCell*) makeShortStringCellFromTableView:(UITableView*)tableView
|
||||||
if (cell == nil || ![cell isKindOfClass:[MCIntegerFieldTableViewCell class]]) {
|
withIdentifier:(NSString*)identifier
|
||||||
// TODO: Figure out how to make this cell generate child views.
|
label:(NSString*)label
|
||||||
return [[MCIntegerFieldTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MCIntegerField"];
|
andInitialValue:(NSString*)initialValue {
|
||||||
} else {
|
MCShortStringFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MCShortStringField"];
|
||||||
return (MCIntegerFieldTableViewCell*)cell;
|
if (!cell || ![cell isKindOfClass:[MCShortStringFieldTableViewCell class]]) {
|
||||||
|
return nil;
|
||||||
}
|
}
|
||||||
|
cell.delegate = self;
|
||||||
|
cell.identifier = identifier;
|
||||||
|
cell.label = label;
|
||||||
|
cell.value = initialValue;
|
||||||
|
|
||||||
|
// TODO: move these to better properties on MCShortStringFieldTableViewCell they should be stored via label and initialValue/value.
|
||||||
|
cell.textField.text = initialValue;
|
||||||
|
cell.textField.placeholder = label;
|
||||||
|
|
||||||
|
return cell;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (MCIntegerFieldTableViewCell*) makeIntegerCellFromTableView:(UITableView*)tableView
|
||||||
|
withIdentifier:(NSString*)identifier
|
||||||
|
label:(NSString*)label
|
||||||
|
andInitialValue:(int)initialValue {
|
||||||
|
MCIntegerFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MCIntegerField"];
|
||||||
|
if (!cell || ![cell isKindOfClass:[MCIntegerFieldTableViewCell class]]) {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
cell.delegate = self;
|
||||||
|
cell.identifier = identifier;
|
||||||
|
cell.label = label;
|
||||||
|
cell.value = initialValue;
|
||||||
|
|
||||||
|
return cell;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#pragma mark - Navigation
|
#pragma mark - Navigation
|
||||||
|
|
||||||
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
||||||
@@ -84,7 +111,7 @@ const int kSectionIndexAbilityScores = 1;
|
|||||||
// * Alignment
|
// * Alignment
|
||||||
return 5;
|
return 5;
|
||||||
case kSectionIndexAbilityScores:
|
case kSectionIndexAbilityScores:
|
||||||
return 0;
|
return 1;
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -109,60 +136,60 @@ titleForHeaderInSection:(NSInteger)section {
|
|||||||
|
|
||||||
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
|
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
|
||||||
|
|
||||||
MCShortStringFieldTableViewCell *shortStringCell = nil;
|
UITableViewCell *newCell = nil;
|
||||||
MCIntegerFieldTableViewCell *integerCell = nil;
|
|
||||||
|
|
||||||
switch (indexPath.section) {
|
switch (indexPath.section) {
|
||||||
case kSectionIndexBasicInfo:
|
case kSectionIndexBasicInfo:
|
||||||
switch (indexPath.row) {
|
switch (indexPath.row) {
|
||||||
case 0:
|
case 0:
|
||||||
shortStringCell = [self makeShortStringCellFromCell:[self.monsterTableView dequeueReusableCellWithIdentifier:@"MCShortStringField"]];
|
newCell = [self makeShortStringCellFromTableView:self.monsterTableView
|
||||||
shortStringCell.delegate = self;
|
withIdentifier:@"monster.name"
|
||||||
shortStringCell.identifier = @"monster.name";
|
label:NSLocalizedString(@"Name", @"Placeholder text for the name of a monster or NPC.")
|
||||||
// TODO: make these use setters on MCShortStringFieldTableViewCell
|
andInitialValue:self.editingMonster.name];
|
||||||
shortStringCell.textField.text = self.editingMonster.name;
|
break;
|
||||||
shortStringCell.textField.placeholder = NSLocalizedString(@"Name", @"Placeholder text for the name of a monster or NPC.");
|
|
||||||
return shortStringCell;
|
|
||||||
case 1:
|
case 1:
|
||||||
shortStringCell = [self makeShortStringCellFromCell:[self.monsterTableView dequeueReusableCellWithIdentifier:@"MCShortStringField"]];
|
newCell = [self makeShortStringCellFromTableView:self.monsterTableView
|
||||||
shortStringCell.delegate = self;
|
withIdentifier:@"monster.size"
|
||||||
shortStringCell.identifier = @"monster.size";
|
label:NSLocalizedString(@"Size", @"Placehodler text for the size of a monster or NPC.")
|
||||||
// TODO: make these use setters on MCShortStringFieldTableViewCell
|
andInitialValue:self.editingMonster.size];
|
||||||
shortStringCell.textField.text = self.editingMonster.size;
|
break;
|
||||||
shortStringCell.textField.placeholder = NSLocalizedString(@"Size", @"Placehodler text for the size of a monster or NPC.");
|
|
||||||
return shortStringCell;
|
|
||||||
case 2:
|
case 2:
|
||||||
shortStringCell = [self makeShortStringCellFromCell:[self.monsterTableView dequeueReusableCellWithIdentifier:@"MCShortStringField"]];
|
newCell = [self makeShortStringCellFromTableView:self.monsterTableView
|
||||||
shortStringCell.delegate = self;
|
withIdentifier:@"monster.type"
|
||||||
shortStringCell.identifier = @"monster.type";
|
label:NSLocalizedString(@"Type", @"Placehodler text for the type of a monster or NPC.")
|
||||||
// TODO: make these use setters on MCShortStringFieldTableViewCell
|
andInitialValue:self.editingMonster.type];
|
||||||
shortStringCell.textField.text = self.editingMonster.type;
|
break;
|
||||||
shortStringCell.textField.placeholder = NSLocalizedString(@"Type", @"Placehodler text for the type of a monster or NPC.");
|
|
||||||
return shortStringCell;
|
|
||||||
case 3:
|
case 3:
|
||||||
shortStringCell = [self makeShortStringCellFromCell:[self.monsterTableView dequeueReusableCellWithIdentifier:@"MCShortStringField"]];
|
newCell = [self makeShortStringCellFromTableView:self.monsterTableView
|
||||||
shortStringCell.delegate = self;
|
withIdentifier:@"monster.subtype"
|
||||||
shortStringCell.identifier = @"monster.subtype";
|
label:NSLocalizedString(@"Subtype", @"Placeholder text for the subtype of a monster or NPC.")
|
||||||
shortStringCell.textField.text = self.editingMonster.subtype;
|
andInitialValue:self.editingMonster.subtype];
|
||||||
shortStringCell.textField.placeholder = NSLocalizedString(@"Subtype", @"Placeholder text for the subtype of a monster or NPC.");
|
break;
|
||||||
return shortStringCell;
|
|
||||||
case 4:
|
case 4:
|
||||||
shortStringCell = [self makeShortStringCellFromCell: [self.monsterTableView dequeueReusableCellWithIdentifier:@"MCShortStringField"]];
|
newCell = [self makeShortStringCellFromTableView:self.monsterTableView
|
||||||
shortStringCell.delegate = self;
|
withIdentifier:@"monster.alignment"
|
||||||
shortStringCell.identifier = @"monster.alignment";
|
label: NSLocalizedString(@"Alignment", @"Placeholder text for the alignment of a monster or NPC.")
|
||||||
shortStringCell.textField.text = self.editingMonster.alignment;
|
andInitialValue:self.editingMonster.alignment];
|
||||||
shortStringCell.textField.placeholder = NSLocalizedString(@"Alignment", @"Placeholder text for the alignment of a monster or NPC.");
|
break;
|
||||||
return shortStringCell;
|
}
|
||||||
|
break;
|
||||||
|
case kSectionIndexAbilityScores:
|
||||||
|
switch (indexPath.row) {
|
||||||
|
case 0:
|
||||||
|
return [self makeIntegerCellFromTableView:self.monsterTableView
|
||||||
|
withIdentifier:@"monster.strengthScore"
|
||||||
|
label:NSLocalizedString(@"STR", @"Placeholder abbreviation for the strength score of a monster or NPC.")
|
||||||
|
andInitialValue:self.editingMonster.strengthScore];
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if DEBUG
|
if (!newCell) {
|
||||||
NSLog(@"ERROR: Unable to build a cell for %@", indexPath);
|
NSLog(@"ERROR: Unable to build a cell for %@", indexPath);
|
||||||
return nil;
|
newCell = [self makeSafeCell];
|
||||||
#else
|
}
|
||||||
return [[UITableViewCell alloc] init];
|
|
||||||
#endif
|
return newCell;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - MCShortStringFieldDelegate
|
#pragma mark - MCShortStringFieldDelegate
|
||||||
@@ -184,6 +211,16 @@ titleForHeaderInSection:(NSInteger)section {
|
|||||||
if ([kMCFieldValueTypeInteger isEqualToString:type]) {
|
if ([kMCFieldValueTypeInteger isEqualToString:type]) {
|
||||||
if ([@"monster.strengthScore" isEqualToString:identifier]) {
|
if ([@"monster.strengthScore" isEqualToString:identifier]) {
|
||||||
self.editingMonster.strengthScore = [(NSNumber*)value intValue];
|
self.editingMonster.strengthScore = [(NSNumber*)value intValue];
|
||||||
|
} else if ([@"monster.dexterityScore" isEqualToString:identifier]) {
|
||||||
|
self.editingMonster.dexterityScore = [(NSNumber*)value intValue];
|
||||||
|
} else if ([@"monster.constitutionScore" isEqualToString:identifier]) {
|
||||||
|
self.editingMonster.constitutionScore = [(NSNumber*)value intValue];
|
||||||
|
} else if ([@"monster.intelligenceScore" isEqualToString:identifier]) {
|
||||||
|
self.editingMonster.intelligenceScore = [(NSNumber*)value intValue];
|
||||||
|
} else if ([@"monster.wisdomScore" isEqualToString:identifier]) {
|
||||||
|
self.editingMonster.wisdomScore = [(NSNumber*)value intValue];
|
||||||
|
} else if ([@"monster.charismaScore" isEqualToString:identifier]) {
|
||||||
|
self.editingMonster.charismaScore = [(NSNumber*)value intValue];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user