Я долго искал это и не получил четкого ответа.
У меня есть UISwitch
как accessoryView
в ячейке представления таблицы. Проблема в том, что каждый раз, когда я прокручиваю таблицу, переключатель возвращается в исходное состояние.
Вот мой метод 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];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
switch1 = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
[switch1 addTarget:self action:@selector(buttonPressed :) forControlEvents:UIControlEventValueChanged];
[switch1 setOn:YES animated:NO];
cell.accessoryView = switch1;
NSString *iconsImage = [[self iconsImages] objectAtIndex:[indexPath row]];
UIImage *cellIcon = [UIImage imageNamed:iconsImage];
[[cell imageView] setImage:cellIcon];
CGRect labelFrame = CGRectMake(65, 18, 150, 25);
UILabel *iconsNameLabel = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
iconsNameLabel.font = [UIFont boldSystemFontOfSize:18];
iconsNameLabel.text = [iconsList objectAtIndex:indexPath.row];
[cell.contentView addSubview:iconsNameLabel];
return cell;
}
Кстати, я объявил свой переключатель в заголовочном файле, установил его как свойство и синтезировал.