2013-09-05 58 views
0

我目前正在研究关注CoreData和自定义单元格的第二个项目。从自定义表格单元格访问UISwitch

我有一个图像的自定义单元格&标签&开关,我试图将交换机的值更改时保存交换机的值到userdefaults。但我坚持如何单独访问每个开关(在我的表视图的相同部分有2个),以便当按下开关时,存储在userdefaults中的整数立即更新。

这是关于自定义单元(switchCell)的.m文件中的代码,对任何杂乱的代码等都表示歉意,我几乎100%自学,没有任何对我犯的错误的建议/反馈。

- (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; 
    } 
} 

} 

在此先感谢您的帮助!

回答

0

为您的SwitchCell添加一个IBAction。

在头文件中加入:

- (IBAction)switchChanged: (UISwitch*)sender; 

而在.m文件规定:

- (IBAction)switchChanged: (UISwitch*)sender { 
    BOOL value = sender.isOn; 

    // Do your Core Data work here 
} 

您可能需要一个属性添加到SwitchCell,这样它才能知道什么样的数据库条目创建/更改。

然后进入Storyboard或.nib文件,单击UISwitch,打开对象检查器中的最后一个选项卡。你应该可以看到Send Events -> Value Changed。从这个连接中拖拽一个连接到SwitchCell,选择你的IBAction,你应该很好走。

+0

蒂姆你为什么假设他使用故事板? :) –

+0

@AuRis他会叫'dequeueReusableCellWithIdentifier:@“SwitchCellIdentifier”'如果他没有使用Storyboard或.nib文件?我可能是错的,但是如果不通过.nib或storyboard,你将如何定义/布置该标识符的单元格? –

+0

嘿,我使用.nib文件,因为学习代码我一直认为故事板不是要走的路;因此为什么要避免他们! –

1

我会在你的子类中添加该开关,而不是在viewController中。然后在单元格子类中创建一个委托,它将在viewController中调用像switchDidChangeValue这样的方法。在那里设置你的默认值。

+0

如果我明白我已经创建了subclassed单元格内的开关,以及一个标签和图像(testSwitch是我测试接近这个问题的非自定义单元格方式,我想避免)。我以前也没有使用过代表,这是100%必要的吗?或者你会说这是最佳做法? –

0

这么多的代码:-),如果我理解你的想法,我就不会很开心。但是我想也许你可以定义你的viewController两个属性的两个开关,switchOne和switchTwo,并赋予它们在功能上

tableView:cellForRowAtIndexPath: 

我总是这样做,当我在分组的tableView独立控制。

不过我对你的代码的一些问题:

  1. 你的意思是第二和第三细胞在你的第一个部分是与交换机细胞?

  2. 我想在你的代码

    如果(indexPath.row == 1 || indexPath。行== 2){
    SwitchCell * switchCell = [tableView dequeueReusableCellWithIdentifier:@“SwitchCellIdentifier”];
    cell = switchCell; } 细胞永远是零,因为你从来没有建立一个细胞与标识符“SwitchCellIdentifier”

  3. 我想你建立你的第一个节的其他单元开关控制,所以我完全不知道你想要

    什么

现在在中国的早上2点,我工作了一整夜,我很努力,所以也许你的代码是正确的,我误解了,如果有的话,请让我知道。

+0

嘿,谢谢你试图理解,我很难解释这个问题,因为它让我相当困惑于我的编码!基本上我没有创建单元格的麻烦,他们都很好,我只是无法访问在我的自定义单元格中创建的开关(第0行1&2,创建的很好)。 –