如何解决一次只允许一个UITableViewCellAccessoryCheckmark
|| 我有一个带有部分视图的表格视图,该部分包含用户可以“拾取”的声音列表。只有当前选择的声音应该显示“ 0”。基本上我可以正常工作。但是,例如底部的单元格被选中,然后向上滚动表格视图(使选中的单元格不可见),然后我检查另一个单元格,底部(不可见)单元格的检查未被删除。有人知道为什么吗?- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static Nsstring *CellIdentifier = @\"Cell\";
static Nsstring *TheOtherone = @\"OtherCell\";
static Nsstring *MiddleCell = @\"MiddleCell\";
// Configure the cell...
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
Nsstring *theSound = [prefs objectForKey:@\"pickedFailSound\"];
NSUInteger index = [failSounds indexOfObject:theSound];
if (indexPath.section == 0){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
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\";
cell.detailTextLabel.text = @\"Fail at Laucnh\";
return cell;
} else if (indexPath.section == 1) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MiddleCell];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MiddleCell] autorelease];
}
Nsstring *cellTitle = [failSounds objectAtIndex:indexPath.row];
cell.textLabel.text = cellTitle;
if (indexPath.row == index) {
cell.accessoryType = UITableViewCellAccessorycheckmark;
}
if ([cellTitle isEqualToString:@\"Your Fail Sound\"]){
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if(![prefs boolForKey:@\"customSoundAvailable\"]) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
cell.textLabel.textColor = [UIColor grayColor];
cell.detailTextLabel.text = @\"Record a Custom Failsound First\";
}
}
return cell;
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TheOtherone];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:TheOtherone] 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;
}
}
和:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
Nsstring *theSound = [prefs objectForKey:@\"pickedFailSound\"];
NSUInteger index = [failSounds indexOfObject:theSound];
//NSLog(@\"The index is: %@\",index);
if (indexPath.section == 1){
NSIndexPath *oldindexPath = [NSIndexPath indexPathForRow:index inSection:1];
NSLog(@\"Oldindexpath is: %@\",oldindexPath);
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
if (newCell.accessoryType == UITableViewCellAccessoryNone) {
newCell.accessoryType = UITableViewCellAccessorycheckmark;
}
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldindexPath];
if (oldCell != newCell){
if (oldCell.accessoryType == UITableViewCellAccessorycheckmark) {
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
}
Nsstring *pickedSound = [failSounds objectAtIndex:indexPath.row];
NSLog(@\"Picked sound is %@\",pickedSound);
[prefs setobject:pickedSound forKey:@\"pickedFailSound\"];
[prefs synchronize];
[self performSelector:@selector(loadSound) withObject:nil];
[failSound play];
}
}
解决方法
您不记得要为出队单元格重置附件类型。
这应该为您解决;
cell.accessoryType = UITableViewCellAccessoryNone;
if (indexPath.row == index) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
实用提示:您可能会发现使用switch(indexPath.section)而不是很多if ... elseif ... else构造很有用。尽管您现在只需要2个部分就可以了,但是我发现使用switch可以更轻松地扩展部分的数量,并且从心理上更容易阅读代码,因为您经常将if .. else结构用于其他事物-使用不同的语法有助于更清楚地说明测试的目的。
,在每次声明cell
之后的ѭ4中,尝试通过添加以下行来重置单元格类型:cell.accessoryType = UITableViewCellAccessoryNone;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。