2012-09-20 39 views
1

我有一個有四行的表格,每行都有一個標題和開關作爲附件。我希望在點擊相應的開關時更新plist布爾值。不確定如何在UITableViewCell中從UISwitch更新Plist中的Bool值

plist中:

<array> 
    <dict> 
     <key>Bools</key> 
     <array> 
      <false/> 
      <false/> 
      <false/> 
      <false/> 
     </array> 
     <key>Strings</key> 
     <array> 
      <string>String0</string> 
      <string>String1</string> 
      <string>String2</string> 
      <string>String3</string> 
     </array> 
    </dict> 
</array> 

這裏是數據源的方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; { 
return [allergens count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; { 
return [[[allergens objectAtIndex: section] objectForKey: @"Strings"] count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; { 
return @"Header"; 
} 

這是我的開關,選擇器:switchToggled

UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero]; 
cell.accessoryView = switchView; 
[switchView addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside]; 

下面是IBAction爲方法。我可以成功地將switch tapped顯示在日誌中,所以我知道一切正常。我只是不知道如何得到正確的布爾值進行更新。

- (IBAction)switchToggled:(id)sender { 
NSLog(@"switch tapped"); 
} 

任何幫助表示讚賞!謝謝。

+0

您需要在開關按下後更新模型。並使用此模型更新開關狀態。 – NeverBe

回答

0

當我發佈這個時,我對編程非常陌生我不知道我無法以編程方式在運行時在主包中編輯plist。我現在明白爲了達到我想要的效果,我應該將開始的plist保存到應用程序文檔目錄並從中解決問題。我使用this文章來幫助格式化文檔目錄中保存和加載數據的過程。