Вы можете использовать insertRowsAtIndexPaths
, это не только добавит, но и сделает анимацию добавления новой строки
сначала добавьте цель UIControlEventTouchUpInside
в UISwitch
.
[switch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
- (void) switchChange: (UISwitch *) sender {
isSwitchOn = sender.on; //edited
[myTable beginUpdates];
NSIndexPath *row1 = [NSIndexPath indexPathForRow:1 inSection:0];
NSIndexPath *row2 = [NSIndexPath indexPathForRow:2 inSection:0];
//you may add as many row as you want here.
[myTable insertRowsAtIndexPaths:[NSArray arrayWithObjects:row1,row2,nil] withRowAnimation:UITableViewRowAnimationMiddle];
[myTable endUpdates];
}
и убедитесь, что вы добавите, сколько строквы вставите в numberOfRowsInSection:
(NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) раздел {
, если (isSwitchOn) вернется1;
, иначе возврат 3;// сколько строк будет после включения
}
Возможно, вы захотите проверить [myTable insertSections:withRowAnimation:
, чтобы вставить другой раздел и сделать то же, что и выше,но не забудьте указать, сколько разделов вы добавите, в numberOfSectionsInTableView:
ОБНОВЛЕНИЕ:
внедрить deleteRowsAtIndexPaths:
если выключить.
- (void)switchChange: (UISwitch *) отправитель {
isSwitchOn = sender.on;
if(isSwitchOn){
[myTable beginUpdates];
NSIndexPath *row1 = [NSIndexPath indexPathForRow:1 inSection:0];
NSIndexPath *row2 = [NSIndexPath indexPathForRow:2 inSection:0];
//you may add as many row as you want here.
[myTable insertRowsAtIndexPaths:[NSArray arrayWithObjects:row1,row2,nil] withRowAnimation:UITableViewRowAnimationMiddle];
[myTable endUpdates];
}
else{
[myTable beginUpdates];
NSIndexPath *row1 = [NSIndexPath indexPathForRow:1 inSection:0];
NSIndexPath *row2 = [NSIndexPath indexPathForRow:2 inSection:0];
//you may add as many row as you want here.
[myTable deleteRowsAtIndexPaths:[NSArray arrayWithObjects:row1,row2,nil] withRowAnimation:UITableViewRowAnimationFade];
[myTable endUpdates];
}
}