2012-03-05 74 views
0

請原諒我,因爲這有點難以解釋。我會盡力的!名稱NSUserDefaults自動

所以,我正在創建書籤。我已經儘可能將WebView的URL添加到您可以設置並顯示它的表格中。這適用於一個書籤。代碼如下所示:

//Gets Page (currentAddress is the address of the webview passed to the bookmarks) 
NSString *address = [defaults objectForKey:@"currentAddress"]; 

//Adds Set URL To Defaults (defaults is the name for the NSUserDefaults declaration) 
[defaults setObject:address forKey:@"savedAddress1"]; 

//Adds To Array (favorites is the mutable array that the table displays) 
[favorites addObject:address]; 

//Syncs Defaults 
[defaults synchronize]; 

//Animate Back To Home Screen 
[self dismissModalViewControllerAnimated:YES]; 

但是,當您添加新書籤時,它們只會覆蓋彼此。你如何能夠連續地命名它們,以便它們不會自動地寫出彼此?請記住,我需要知道如何獲取刪除索引,並檢查它是否爲零。

所以,如果你有什麼可以幫助我的話,那就太好了。我在想也許在NSUserDefaults中使用一個數組來跟蹤,但是我又不知道,我需要你的幫助!

感謝您的時間!

--Jake

+0

'NSArray'很好的堅持'NSUserDefaults'。 – 2012-03-05 01:15:38

+0

太棒了!只是..你將如何自動命名數組的每個部分? – JTApps 2012-03-05 01:16:59

回答

1

如果你的數據結構變得太複雜,你應該考慮存儲的另一種方法。

您可能會忘記使用NSDictionary,其中Key是書籤的名稱,值是書籤地址。

{ 
    "Google" = "http://google.com" 
} 

當你加載您應將數據源搶字典從NSUserDefaults

self.bookmarks = [[userDefaults dictionaryForKey:URDictionaryKey] mutableCopy]; 

爲了使您的表格,以便你可以創建一個排序的字典鍵的NSArray

self.bookmarkKeys = [[self.bookmarks allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; 

爲了您的數據源方法,您使用類似

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 
{ 
    return [self.bookmarkKeys count]; 
} 

對於單元配置你使用這樣的:如果用戶在表中刪除行,你會

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    NSString *title = [self.bookmarkKeys objectAtIndex:indexPath.row]; 
    cell.textLabel.text  = title; 
    cell.detailTextLabel.text = [self.bookmarks objectForKey:title]; 

    return cell; 
} 

做類似的事情:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     NSString *key = [self.bookmarkKeys objectAtIndex:indexPath.row]; 
     [self.bookmarks removeObjectForKey:key]; 

     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
     [defaults setObject:self.bookmarks forKey:URDictionaryKey]; 
     [defaults synchronize]; 

    } 
} 

更新

你可能會需要使用NSMutableDictionary它是NSDictionary子類,所以在看文檔兩個將有助於你的理解。

在這種情況下,你將被空可變字典開始

self.bookmarks = [NSMutableDictionary dictionary]; 

然後,當用戶添加書籤您使用的名稱爲key和目標爲value

它添加到字典
[self.bookmarks setObject:@"http://google.com" forKey:@"Google"]; 
+0

相信我,作爲一個初學者(我已經使用XCode約2個月),這將是艱難的。我從來沒有使用NSDictionary之前。剩下的答案似乎很簡單,但你如何設置NSDictionary?謝謝! = D – JTApps 2012-03-05 01:43:46

+0

我已經走了一個錯綜複雜的布爾系統。它檢查條目是否爲零,如果是,則在那裏添加書籤。我遇到的唯一問題是刪除它們..你如何判斷哪一行被刪除,以便我可以將該行的NSDefault設置爲零? – JTApps 2012-03-05 02:26:30

+0

'tableView:commitEditingStyle:forRowAtIndexPath'爲您提供indexPath。很難想象你的設置沒有看到一些代碼,所以我不能真正幫助太多 – 2012-03-05 09:08:49