Надеюсь, кто-нибудь сможет мне помочь.
У меня есть контроллер навигации для нескольких контроллеров таблиц. Одно из представлений таблицы («таблица привязок») имеет ячейки с дополнительными представлениями (переключатели и ползунки).
'Таблица привязок' загружается нормально, однако, когда я помещаю новый viewController в стек, а затем возвращаюсь к 'таблице привязок', некоторые ячейки, которые ранее не имели дополнительных представлений, теперь имеют переключатель или ползунок .
В методе viewDidLoad для anchorsViewController я установил переменные экземпляра для переключателей, а ползунки = nil. Вот мой метод cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Anchors"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
// Create UI Elements only once by checking to see if
// one of them is nil
if (displayAnchorsSwitch == nil) {
// Display Anchors
if (indexPath.section == 0) {
NSLog(@"Creating displayAnchorsSwitch");
self.displayAnchorsSwitch = [[[UISwitch alloc]
initWithFrame:CGRectZero]autorelease];
self.displayAnchorsSwitch.on = NO;
cell.accessoryView = self.displayAnchorsSwitch;
[self.displayAnchorsSwitch addTarget:self
action:@selector(switchDisplayAnchors:)
forControlEvents:UIControlEventValueChanged];
}
}
if (anchorsLineWidthSlider == nil) {
// Line Width
if (indexPath.section == 1 && indexPath.row == 0) {
NSLog(@"Creating anchorsLineWidthSlider");
self.anchorsLineWidthSlider = [[[UISlider alloc]
initWithFrame:CGRectMake(0.0, 0.0, 200.0, 0.0)]autorelease];
self.anchorsLineWidthSlider.continuous = YES;
self.anchorsLineWidthSlider.value = 0.25;
cell.accessoryView = self.anchorsLineWidthSlider;
[self.anchorsLineWidthSlider addTarget:self
action:@selector(sliderAnchorsLineWidth:)
forControlEvents:UIControlEventValueChanged];
}
}
// Line Opacity
if (indexPath.section == 1 && indexPath.row == 1) {
if (anchorsLineOpacitySlider == nil) {
NSLog(@"Creating anchorsLineOpacitySlider");
self.anchorsLineOpacitySlider = [[[UISlider alloc]
initWithFrame:CGRectMake(0.0, 0.0, 200.0, 0.0)]autorelease];
self.anchorsLineOpacitySlider.continuous = YES;
self.anchorsLineOpacitySlider.value =0.75;
cell.accessoryView = self.anchorsLineOpacitySlider;
[self.anchorsLineOpacitySlider addTarget:self
action:@selector(sliderAnchorsLineOpacity:)
forControlEvents:UIControlEventValueChanged];
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if (indexPath.section == 1 && indexPath.row == 2) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (indexPath.section == 1 && indexPath.row == 3) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (anchorsFillSwitch == nil) {
// Display Fill
if (indexPath.section == 2 && indexPath.row == 0) {
NSLog(@"Creating anchorsFillSwitch");
self.anchorsFillSwitch = [[[UISwitch alloc]
initWithFrame:CGRectZero]autorelease];
self.anchorsFillSwitch.on = YES;
cell.accessoryView = self.anchorsFillSwitch;
[self.anchorsFillSwitch addTarget:self
action:@selector(switchAnchorsFill:)
forControlEvents:UIControlEventValueChanged];
}
}
if (indexPath.section == 2 && indexPath.row == 1) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (indexPath.section == 2 && indexPath.row == 2) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}