У меня есть UITableView, содержащий три раздела.
- Раздел 1 имеет одну ячейку, содержащую UISwitch
failAtLaunch
в своем дополнительном представлении - Раздел 2 содержит элементы из массива failSounds и имеет единственную галочку для выбранного элемента (звук вв этом случае)
- В разделе 3 есть кнопка.
Ниже показано, как я настраиваю ячейки.
Проблема в том, что таблица дает странное поведение,Всякий раз, когда я прокручиваю вверх и вниз табличное представление с перемещением ячеек внутрь и из вида, некоторые ячейки в разделе 2 получают UISwitch failAtLaunch
в своем вспомогательном представлении.
Может кто-нибудь помочь мне понять, почему это так?
Заранее спасибо!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//....blablabla...
// Configure the cell...
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *theSound = [prefs objectForKey:@"pickedFailSound"];
NSUInteger index = [failSounds indexOfObject:theSound];
if (indexPath.section == 0){
failAtLaunch = [[[UISwitch alloc] initWithFrame:CGRectMake(200, 7, 100, 30)] autorelease];
[cell addSubview:failAtLaunch];
cell.accessoryView = failAtLaunch;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if(![prefs boolForKey:@"failAtLaunch"]) {
[failAtLaunch setOn:NO animated:NO];
} else {
[failAtLaunch setOn:YES animated:NO];
}
[(UISwitch *)cell.accessoryView addTarget:self action:@selector(setIt) forControlEvents:UIControlEventValueChanged];
cell.textLabel.text = @"Instafail";
return cell;
} else if (indexPath.section == 1) {
NSString *cellTitle = [failSounds objectAtIndex:indexPath.row];
cell.textLabel.text = cellTitle;
if (indexPath.row == index) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"buttonCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"buttonCell"] autorelease];
}
cell.textLabel.text = @"Hold to record fail sound";
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(230.0f, 4.0f, 60.0f, 36.0f);
[btn setTitle:@"OK" forState:UIControlStateNormal];
[btn setTitle:@"OK" forState:UIControlStateSelected];
[btn addTarget:self action:@selector(recordAudio) forControlEvents:UIControlEventTouchDown];
[btn addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
return cell;
}
}