2011-09-29 10 views
-1

我有一個基於標籤欄的應用程序,在選項卡上有一個TableViewController如何使用NSUserDefaults來存儲行屬性

當我更新我的應用程序時,我向我的Table添加了一個新行。

我想要做的是存儲我的TableViewController一行的突出顯示顏色,只要我插入一個新的,至少3個會話。

我知道我必須使用NSUserDefaults,但是,在哪裏? 我的意思是:我應該把它用於我的rowViewController(這是一個UIViewController)還是我的TableViewController(我有所有的行)?

我怎樣才能將單元格屬性導入UIViewController(如果在那裏我必須使用NSUserDefaults)?

我有類似:

- (void) applicationDidFinishLaunching:(NSNotification*) notice { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:cell]; 
    if ([(NSInteger*)[[NSUserDefaults standardUserDefaults] integerForKey:cell] intValue] < 3) { 
     // render highlighted... 
    } else { 
     // render normal 
    } } 

我不能做「導致它的一個UIViewController,對不對?然後在

@interface rowViewController : UIViewController 

的.H我應該爲了實現細胞添加的東西,然後設置其屬性(顏色)?

在此先感謝。

+1

我不清楚爲什麼你需要使用NSUserDefaults。它總是一樣的顏色?你需要在應用程序會話之間存儲顏色嗎? – sosborn

+0

我需要使用NSUserDefaults,以便當我添加一個帶有背景顏色的新行(例如黃色)時,它會一直保持黃色3個會話。 – Phillip

回答

1

你可以像你在你的問題中那樣做,但是你的代碼存在一個大問題。你使用NSInteger就好像它是一個對象,而它是一個原子類型。

不要混淆NSIntegerNSNumber

  • NSNumber是一類,其目的是在一個Objective-C的對象封裝的數字,這樣就可以使用它在其中請求NSObjects(就像當你想存儲他們在NSDictionaries
  • NSInteger是一個整數類型的別名(typedef)。以與您使用int相同的方式使用它。

因此,在你的代碼,NSUserDefaultssetInteger:forKey:integerForKey:使用NSInteger的值(即原子值,而不是對象)。不需要使用*intValue然後檢索真正的價值,你已經擁有它! (喜歡你的代碼寫了它必將崩潰,解釋了整數值指向一個內存地址!)


此外,您不能使用電池作爲NSUserDefaults的關鍵NSUserDefaults只接受密鑰的字符串,此外UITableViewCells被重複使用(每次滾動時都回收)並且不會持續。您應該建立一個字符串,使用您單元格的NSIndexPath並將其用於NSUserDefaults鍵;然後將一個單元格/行的背景顏色存儲在UITableView中的一個給定位置(並且無論如何,這是MVC設計模式中模型和視圖的更好分離)。

// Build a string from the IndexPath that will be used as a unique key for your userdefaults, to store info on the corresponding row in your tableview 
NSString* key = [NSString stringWithFormat:@"bkg_%i:%i",indexPath.section,indexPath.row]; 
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:key]; 
if ([[NSUserDefaults standardUserDefaults] integerForKey:key] < 3) 
{ 
    // render highlighted... 
} else { 
    // render normal 
} 

注意integerForKey:可以看作到objectForKey:一個呼叫,隨後到integerValue一個呼叫(將在你的情況下,這是一個對象封裝的整數值返回一個NSObject,典型地爲NSNumber) 。

// These lines... 
NSNumber* nbrObj = [[NSUserDefaults standardUserDefaults] objectForKey:@"mykey"]; 
NSInteger nbr = [nbrObj integerValue]; 
// are equivalent to the following, shorter line: 
NSInteger nbr = [[NSUserDefaults standardUserDefaults] integerForKey:@"mykey"]; 
+0

謝謝,我會盡力讓出一些東西 – Phillip

+0

你可以在你的類中直接使用'NSUserDefaults'來實現'UITableViewDelegate'協議(通常是你的'UITableViewController')。不需要將你的單元格「導入」到你的UIViewController中(這實際上並不意味着什麼)。請閱讀Apple文檔中的「Table View Programming Guide」,它將幫助您深入理解「UITableViews」的機制,包括佈局/顯示週期和重用機制(單元重用/循環使用)。 – AliSoftware

+0

也閱讀有關MVC設計模式的文檔,它將幫助您瞭解如何從「顯示」和與「模型」和元數據相關的信息中分離信息。 PS:我編輯了我的答案,添加了一些示例代碼 – AliSoftware

相關問題