Ios 从自定义表格单元格访问UISwitch
我目前正在进行我的第二个项目,重点是CoreData和自定义单元格 我有一个带有图像、标签和开关的自定义单元格,当开关的值发生更改时,我试图将开关的值保存为userdefaults。但我一直在研究如何单独访问每个开关(在我的表视图的同一部分中有2个),这样当按下开关时,存储在userdefaults中的整数会立即更新 这是.m文件中关于自定义单元格(switchCell)的代码,对于任何混乱的代码等表示歉意,我几乎100%自学成才,没有任何关于我所犯错误的建议/反馈Ios 从自定义表格单元格访问UISwitch,ios,core-data,Ios,Core Data,我目前正在进行我的第二个项目,重点是CoreData和自定义单元格 我有一个带有图像、标签和开关的自定义单元格,当开关的值发生更改时,我试图将开关的值保存为userdefaults。但我一直在研究如何单独访问每个开关(在我的表视图的同一部分中有2个),这样当按下开关时,存储在userdefaults中的整数会立即更新 这是.m文件中关于自定义单元格(switchCell)的代码,对于任何混乱的代码等表示歉意,我几乎100%自学成才,没有任何关于我所犯错误的建议/反馈 - (UITableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
// Code for the first section, controlling which cells use the custom cell SwitchCell
if (indexPath.section == 0)
{
if(indexPath.row == 1 || indexPath.row == 2)
{
SwitchCell *switchCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCellIdentifier"];
cell = switchCell;
}
else
{
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CellIdentifier"];
UISwitch *testSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = testSwitch;
}
}
}
// Each other section currently uses the standard cell type
else
{
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CellIdentifier"];
}
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary = [settingsTableData objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Settings"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
SwitchCell *switchCell = (SwitchCell *)cell;
[[NSUserDefaults standardUserDefaults] synchronize];
// Integers to store the on/off state of each switch (2)
NSInteger capsValue = [[NSUserDefaults standardUserDefaults] integerForKey:@"capitalsSwitchOn"];
NSInteger numbersValue = [[NSUserDefaults standardUserDefaults] integerForKey:@"numbersSwitchOn"];
NSLog(@"Upon load capsValue equals : %d", capsValue);
NSLog(@"upon load numbersValue equals : %d", numbersValue);
// Setting individual cell values for attributes such as image and text
if (indexPath.section == 0)
{
if(indexPath.row == 1)
{
switchCell.switchCellLabel.text = cellValue;
switchCell.switchCellImage.image = [UIImage imageNamed:@"capitalsImage.jpg"];
// Set to true or false depending on what you want the default value to be.
//switchCell.switchCellSwitch.on = FALSE;
if (capsValue == 1) {
[switchCell.switchCellSwitch setOn:NO animated:YES];
} else
[switchCell.switchCellSwitch setOn:YES animated:YES];
}
else if (indexPath.row == 2)
{
switchCell.switchCellLabel.text = cellValue;
switchCell.switchCellImage.image = [UIImage imageNamed:@"capitalsImage.jpg"];
if (numbersValue == 1) {
[switchCell.switchCellSwitch setOn:NO animated:YES];
} else
[switchCell.switchCellSwitch setOn:YES animated:YES];
}
else
{
cell.textLabel.text = cellValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
else if (indexPath.section == 1)
{
if (indexPath.row == 0)
{
cell.textLabel.text = cellValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else if (indexPath.row == 1)
{
cell.textLabel.text = cellValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
else if (indexPath.section == 2)
{
if (indexPath.row == 0)
{
cell.textLabel.text = cellValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else if (indexPath.row == 1 || indexPath.row == 2)
{
cell.textLabel.text = cellValue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
}
提前感谢您的帮助 我会在子类单元格中添加该开关,而不是在viewController中。然后在cell子类中创建一个委托,该委托将在viewController中调用类似于
switchDidChangeValue
的方法。设置默认值。将iAction添加到您的开关单元中
在头文件中添加:
- (IBAction)switchChanged: (UISwitch*)sender;
并在.m文件中应用:
- (IBAction)switchChanged: (UISwitch*)sender {
BOOL value = sender.isOn;
// Do your Core Data work here
}
您可能需要将属性添加到SwitchCell
,以便它知道要创建/更改的数据库条目
然后进入故事板或.nib文件,单击UI开关,打开对象检查器中的最后一个选项卡。在那里,您应该能够看到发送事件->值更改
。将一个连接从那里拖到你的开关单元
,选择你的iAction,你就可以开始了。这么多代码:-)如果我了解你的想法与否,我就不介意了。但我认为也许可以在viewController中为两个开关定义两个属性,即switchOne和switchTwo,然后在函数中指定它们
tableView:cellForRowAtIndexPath:
当分组的tableView中有独立控件时,我总是这样做
我仍然对您的代码有一些疑问:
你是说第一部分的第二个和第三个单元是带开关的单元吗
我认为在你的代码中
如果(indexPath.row==1 | | indexPath.row==2){
SwitchCell*SwitchCell=[tableView dequeueReusableCellWithIdentifier:@“SwitchCellIdentifier”];
电池=开关电池;
}
该单元格将始终为零,因为您从未构建标识符为“SwitchCellIdentifier”的单元格
我认为你为第一部分的其他单元建立了开关控制,所以我完全不知道你想要什么
在中国,现在是凌晨2点,我工作了一整晚,我很累,所以也许你的代码是对的,我误解了,如果是,请告诉我。Tim你为什么认为他使用故事板?:)@如果AuRis没有使用故事板或.nib文件,他会调用dequeueReusableCellWithIdentifier:@“SwitchCellIdentifier”
?我可能是错的,但是如果不是通过.nib或storyboard,你会如何定义/布局该标识符的单元格?嘿,我使用的是.nib文件,自从学习编码以来,我一直觉得storyboard不是我要走的路;所以我才避开他们@如果我包括一些截图会有帮助吗?开关存在的唯一nib是在它的子类中,我了解IBAction业务,但如何区分不同的单元?因为它们都会更改不同的UserDefaults值。抱歉,如果这让事情变得混乱,我会尽力解释!这就是我向开关单元添加属性的意思。在表格视图:cellforrowatinexpath:
中创建单元格时,可以为该属性设置一个值(例如,要更改其值的键),并基于该值设置用户默认值。但是请记住,NSUserDefaults与核心数据无关。如果我知道我已经在子类单元格中创建了开关,以及标签和图像(testSwitch是我测试处理此问题的非自定义单元格方式,我希望避免)。我以前也没有使用过代理,这是100%必要的吗?或者你会说这是最佳实践吗?嘿,谢谢你试着理解,我很难解释这个问题,因为它让我对我的编码感到困惑!基本上,我创建单元格没有问题,它们都很好,我只是没有办法访问在自定义单元格中创建的开关(第0节第1行和第2行,创建得很好)。