我已经实现了一个带有自定义单元格的表格视图。
1.自定义单元格
自定义单元格具有显示日期的标签和用于设置警报的 UI 开关。
当我们打开 uiswitches 时,我的表格视图看起来像这样,
2.表格视图
When user scrolls down the table view the bottom switches are turned off
向上滚动时同样的问题。
为什么会出现这个问题?以及如何预防?
索引路径方法中的行单元格
- (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;
}