我有一个 TableView
和一个自定义单元格,该单元格具有 UILabel
和 UISwitch
。 UISwitch 在 Custom Cell 中声明。我想在 TableView 的 ViewDidLoad
中调用这个开关。
我想使用 NSUserDefaults
来保存 UISwitch 的状态。要加载值,我必须在 ViewDidLoad 中编写代码。
@interface LabelSwitchCustomCell : UITableViewCell {
IBOutlet UILabel *mainLabel;
IBOutlet UISwitch *switchButton;
}
@property (nonatomic, retain) IBOutlet UILabel *mainLabel;
@property (nonatomic, retain) IBOutlet UISwitch *switchButton;
@end
最佳答案
您可以在代码的任何位置将 UISwitch 的状态保存在 NSUserDefaults 中。 您可以通过这种方式设置/加载开关状态:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Set switch states
if (indexPath.row == 0)
cell.switchBtn.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"Switch1State"];
else if(indexPath.row == 1)
cell.switchBtn.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"Switch2State"];
else if(indexPath.row == 2)
cell.switchBtn.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"Switch3State"];
// Set switch states
// Set action for your switch
[cell.switchBtn addTarget:self action:@selector(switchingBtn:) forControlEvents:UIControlEventValueChanged];
}
关于iphone - 如何在 Table View ViewDidLoad 方法中调用自定义单元格中声明的 UISwitch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14664874/