I have implemented a tableview with a custom cell.
1.Custom cell

Custom cell having a label to display date and an UI Switch to set alarms.
My table view appears like like this when we switch on the uiswitches,

2.Table view
When user scrolls down the table view the bottom switches are turned off

Same problem when scrolls up.

why this problem happens ? and how to prevent it?
Cell for row at index path method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Note: I set the cell's Identifier property in Interface Builder to DemoTableViewCell.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
}
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//To display custom cell with label and switch
if(indexPath.row< [appDelegate.alarmsArray count])
{
ReminderCell *cell = (ReminderCell *)[tableView dequeueReusableCellWithIdentifier:CellClassName];
if (!cell)
{
NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
cell = [topLevelItems objectAtIndex:0];
}
cell.reminderSwitch.tag = indexPath.row;
NSMutableDictionary *dict = [appDelegate.alarmsArray objectAtIndex:indexPath.row];
cell.label.text = [dict objectForKey:@"string"];
//=========================================
cell.reminderSwitch.on = NO;
//=========================================
return cell;
}
//Add + button on the last row of uitableview cell..
if(indexPath.row == [appDelegate.alarmsArray count])
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
button.frame = CGRectMake(114, 9.5, 33, 33);
[button addTarget:self action:@selector(AddNewRow:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button ];
return cell;
}
return cell;
}
1 Answer