我正在尝试使用 UISwitch 为某些特定行创建 tableview,并为其余单元格创建 AccessoryDisclosureIndicator。
我在下面的代码中进行了一次检查,得到了想要的结果。但问题是,当我突然滚动表格视图时,开关的位置会改变..这是不希望的,我该如何摆脱这个。
请务必回复此主题
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
int _fontSize = fontSize;
NSArray *switchSettings = [[NSUserDefaults standardUserDefaults] arrayForKey:@"switchGeneralSettings"];
NSLog(@"switch settings :%@",switchSettings);
static NSString *cellIdentifier = @"generalSettingsTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSLog(@"*******************1*************");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
// code to additionally configure cell for multiple other UI components..
if(indexPath.row == 3 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 7)
{
NSLog(@"switch*******************%d*************",indexPath.row);
switchObj = [[UISwitch alloc] initWithFrame:CGRectZero];
switchObj.tag = indexPath.row + 20002;
cell.accessoryView = switchObj;
[switchObj addTarget: self action: @selector(flip:) forControlEvents:UIControlEventValueChanged];
[switchObj release];
}
else
{
NSLog(@"indicator*******************%d*************",indexPath.row);
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
else
{
if(indexPath.row == 3 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6 || indexPath.row == 7)
{
NSLog(@"*******************2*************");
countSwitch = 0;
switchObj = (UISwitch *)[cell.contentView viewWithTag:indexPath.row + 20002];
// switch
[switchObj setOn:NO];
if ([switchSettings count]>0) {
NSString *settingState = [switchSettings objectAtIndex:countSwitch];
if ([settingState isEqualToString:@"ON"]) {
[switchObj setOn:YES];
}
}
countSwitch++;
}
}
// confiure the cell here...
NSLog(@"*******************3*************");
NSString *cellTextLabel = [listGeneralSettings objectAtIndex:indexPath.row];
cell.textLabel.text = cellTextLabel;
cell.textLabel.font = [UIFont systemFontOfSize:_fontSize];
cell.detailTextLabel.text = [listDetailGeneralSettings objectAtIndex:indexPath.row];
NSLog(@" cell.detailTextLabel.text :%@", cell.detailTextLabel.text);
NSLog(@"*******************end*************");
return cell;
}