2013-09-05 103 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,創建的很好)。 –