我有一个 UITableView 包含三个部分。
failAtLaunch
第 1 节的一个单元格在其附件视图中包含一个 UISwitch- 第 2 部分包含来自 failSounds 数组的项目,并且对所选项目(在本例中为声音)有一个单数复选标记
- 第 3 节包含一个按钮。
您可以在下面看到我如何配置单元格..
问题是该表给出了奇怪的行为。每当我向上和向下滚动表格视图时,单元格会移入和移出视图,第 2 节中的某些单元格会failAtLaunch
在其附件视图中获取 UISwitch。
谁能帮我理解这是为什么?
提前致谢!
- (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;
}
}