Thanks guys for the fantastic discussion.
Let me answer Matthew's question first (and Gary's too to a big extent).
When the user changes the switch to ON I would like a SCTextFieldCell to appear below the SCSwitchCell allowing the user to enter data. Likewise, when the user changes the switch to OFF I would like the SCTextFieldCell to disappear.
All the code below assumes that:
1- Your object has a property called "switchPropertyName" for the SCSwitchCell.
2- Your object has a property called "textPropertyName" for the SCTextFieldCell.
3- Both properties are included in the class definition (auto generated), and the text field cell comes right under the switch cell.
First, assuming that the switch cell is in an automatically generated detail view, I'll hide the text field if the initial value of the switch is off in detailViewWillAppear:
- (void)tableViewModel:(SCTableViewModel *)tableViewModel
detailViewWillAppearForRowAtIndexPath:(NSIndexPath *)indexPath
withDetailTableViewModel:(SCTableViewModel *)detailTableViewModel
{
// Enable delegate methods to get called for the detail model (e.g. valueChangedForRowAtIndexPath)
detailTableViewModel.delegate = self;
SCObjectSection *section = (SCObjectSection *)[detailTableViewModel sectionAtIndex:0];
SCSwitchCell *switchCell = (SCSwitchCell *)[section cellForPropertyName:@"switchPropertyName"];
if(switchCell.switchControl.on == FALSE)
{
// remove the text field cell (assuming it's right under the switch cell)
NSInteger index = [section indexForCell:switchCell];
[section removeCellAtIndex:index+1];
}
}
Now once the table is loaded, I'll need to detect whenever the switch value changes in valueChangedForRowAtIndexPath:
- (void)tableViewModel:(SCTableViewModel *)tableViewModel
valueChangedForRowAtIndexPath:(NSIndexPath *)indexPath
{
SCTableViewCell *cell = [tableViewModel cellAtIndexPath:indexPath];
if([cell isKindOfClass:[SCSwitchCell class]])
{
SCTableViewSection *section = [tableViewModel sectionAtIndex:indexPath.section];
if( ((SCSwitchCell *)cell).switchControl.on )
{
SCTextFieldCell *textFieldCell = [SCTextFieldCell cellWithText:@"Enter Text" withPlaceholder:nil
withBoundObject:cell.boundObject withTextFieldTextPropertyName:@"textPropertyName"];
[section insertCell:textFieldCell atIndex:indexPath.row+1];
}
else
{
[section removeCellAtIndex:indexPath.row+1];
}
}
// reload the section
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
[tableViewModel.modeledTableView reloadSections:indexSet
withRowAnimation:UITableViewRowAnimationBottom];
}
Gary: You only need to implement the second part of the code, in the tableViewModel:didSelectRowAtIndexPath: method. Instead of testing switchControl.on, you'll create a BOOL variable that you'll toggle whenever the cell gets selected.
Hope this helps guys!