Hiding the table view cells with switch ON/OFF in iPhone
iphone, objective-c, xcode
Solution
numberOfRows dataSource method - make it return 1 and reloadTableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/// ....
self.switchView = [[[UISwitch alloc] initWithFrame:CGRectZero]autorelease];
cell.accessoryView = self.switchView;
[self.switchView setOn:NO animated:NO];
[self.switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
//....
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if [self.switchView.on]
return 1;
else
{
// normal code return
}
}
- (void) switchChanged:(id)sender {
[self.tableView reloadData];
}
Problem
This may be a simple question for you experts but I can't figure out the correct solution. In my app I created a a table view to list a no of items. In the first row of the cell there a switch (`ON/OFF`). So my client need to hide the bottom cells(other than zeroth cell) when switch is `OFF` and show all the cells when switch is `ON`. I created the switch through code. Can anyone please help me with how to do this? Thanks in advance.