2014-03-31 38 views
0

關於此主題有許多問題,我一直在嘗試使用各種選項數小時,但我需要一些真正的幫助。使用NSUserDefaults持久保留UITableView所選單元格的UIAccessoryType

主要前提是:我有一個Tab Bar controller有兩個選項卡;第一個是Timeline,第二個是In-App Settings(均爲table view controllers)。在In-App settings中,當我選擇「鍵盤」時,我被帶有兩個靜態單元格的光線和黑暗帶到另一個表格視圖。

我想實現的是:當我點擊其中一個單元格時,它會在該cell上放置一個checkmark accessory視圖。

使用NSUserDefaults,我想維護所選單元格並將該單元格的accessoryType設置爲在下一個視圖重新加載時勾選。

我跟着這個問題'選擇的答案,而我的代碼允許我選擇光或暗單元;它不會在重新加載之間保持這種狀態。

這裏是我的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];  
    self.selectedKeyboardCell = [[NSUserDefaults standardUserDefaults] objectForKey:@"Selected"]; 

    NSLog(@"The selected keyboard is %@", self.selectedKeyboardCell); 

    if([self.checkedIndexPath isEqual:indexPath]) 
    { 
     NSLog(@"Loaded"); 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 

    } 
    else 
    { 
     NSLog(@"Not Loaded"); 

     cell.accessoryType = UITableViewCellAccessoryNone; 
     /*if ([self.selectedKeyboardCell isEqualToString:@"Light"]) 
     { 
      NSLog(@"P"); 
      if (indexPath.row == 0) 
      { 
       NSLog(@"Q"); 
       cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      } 
      cell.accessoryType = UITableViewCellAccessoryNone; 

     } 
     else 
     { 
      NSLog(@"R"); 
      if (indexPath.row == 1) 
      { 
       NSLog(@"Z"); 
       cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      } 
      cell.accessoryType = UITableViewCellAccessoryNone; 
*/ 

    } 

didSelectRowAtIndexPath是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Uncheck the previous checked row 
    if(self.checkedIndexPath) 
    { 
     UITableViewCell* uncheckCell = [tableView 
             cellForRowAtIndexPath:self.checkedIndexPath]; 
     uncheckCell.accessoryType = UITableViewCellAccessoryNone; 
    } 
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    self.checkedIndexPath = indexPath; 

    self.selectedKeyboardCell = cell.textLabel.text; 

    [[NSUserDefaults standardUserDefaults] setObject:self.selectedKeyboardCell forKey:@"Selected"]; 

    self.selectedKeyboardTheme = cell.textLabel.text; 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 


    [[NSUserDefaults standardUserDefaults] setObject:self.selectedKeyboardTheme forKey:@"Keyboard"]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
} 

我在viewWillAppear一個[self.keyboardTableView reloadData];

我已經評論了一些我一直在嘗試的代碼,但無論我讀了多少帖子,我都找不到解決方案。

這是關於移碼到viewWillAppear中的問題談判,但我不知道他在說什麼代碼:Ho to refresh view and tableview, and checkmark a default setting cell on load?

問題:我想選擇的表視圖細胞和具有細胞有選配件類型選擇下次(每次加載這個視圖)。這是一張靜態桌子,文字只會說光明或黑暗。

cellForRowAtIndexPath,self.selectedKeyboardCell準確地告訴我它是光還是暗,取決於視圖加載之前選擇哪個單元格。但是,如何說「如果Light,在第一個單元格上設置複選標記,並從第二個單元格中刪除複選標記,並且如果爲Dark,則在SECOND單元格上設置複選標記並從第一個單元格中刪除複選標記。爲了清楚起見,我可以選擇Light或Dark靜態表格視圖單元格和我選擇的任何單元格,複選標記都會正確顯示。問題是當我轉到另一個視圖並回到這個視圖時,先前選擇的單元格不顯示。我將所選單元格的textLabel保存爲NSUserDefaults,並保存在NSLog中,它將顯示所選單元格的文本,但不會顯示所選單元格上的複選標記。

更新二: 爲了設置默認選定單元點燃第一次應用在跑,我的確在的cellForRowAtIndexPath如下:

if (!self.selectedKeyboardCell) 
{ 
    self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 

任何指導,在此將受到嚴重讚賞。

回答

1

老實說你的代碼有點混亂,如果你多說了一些實際工作的東西,它會有所幫助。但是:

viewWillAppear你應該只設置self.checkedIndexPath

if ([self.selectedKeyboardCell isEqualToString:@"Light"]) 
{ 
    self.checkedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
}else{ 
    self.checkedIndexPath = [NSIndexPath indexPathForRow:1 inSection:0]; 
} 
[self.keyboardTableView reloadData]; 

吧?

+0

尊敬的@Nils - 我對我的問題缺乏清晰度表示歉意,但您的回答完美地完成了這項工作。我會更新我的問題以確保有清晰的內容。儘管我有一個快速跟進問題。如果我卸載並重新安裝我的應用程序,默認單元格的複選標記爲「黑暗」;我希望它是「Light」單元格(它是indexPathForRow:0),我試圖將它添加到故事板上,這是Light Cell的複選標記,但它沒有什麼區別,我將如何設置默認選擇狀態(第一次運行應用程序)到Light? – amitsbajaj

+0

實際上,我剛剛得到它的工作,並使用Update Two更新了我的問題以反映完成的代碼。我非常感謝你@Nils - 你已經救了我幾小時和幾小時,你已經幫我做到了這一點,我真的很感激,如果可以的話,我會給你兩次投票。 – amitsbajaj

+0

哈哈,不客氣;) –

相關問題