【发布时间】:2014-07-29 18:00:21
【问题描述】:
我正在使用以下代码将 UISwitch 添加到 UITableView 单元格,但遇到了一个奇怪的显示问题。问题似乎是因为我会定期自动刷新 TableView,从而重新创建 UISwitch。
图片最能说明正在发生的事情。
开头是这样的:
变成这样:
顶部开关被打开和关闭。进入此状态需要几分钟时间(带有黑色绒毛),但只要打开然后关闭 UISwitch,您就会发现问题。
这里是代码:-
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//Includes code that also adds a UITextView and a UIImageView, but these do not have issues.
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(10, 5.0, 60.0, 50.0)];
[switchView addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:switchView];
return cell;
【问题讨论】:
-
这是在设备上还是在模拟器上?什么操作系统版本?
-
这是在设备上,IOS7。 iPad Mini Retina,运行 iPhone 应用程序。
-
这是一个“单元重用错误”的示例。您可能还注意到,当您将开关滚动到屏幕外时,它们会自行重置。
-
CrimsonChris - 这是框架或我的代码的错误吗?我还没有找到解决办法。
-
@Remixed123 这是您的代码的错误。请参阅我的答案以获取可能的解决方案。子类化
UITableViewCell是另一种可能的解决方案。
标签: ios objective-c uitableview uiswitch