2010-08-16 43 views

回答

1

ALX,

一個簡單的場景是創建一個基於NIB UITableViewCell(也有很多教程在那裏爲這個),其中有某種它的標籤。

你可以做的是抓取標籤的內容,當用戶選擇單元格並將其存儲到一個可變數組中時,然後被存儲到NSUserDefaults中。

然後,您可以從另一個視圖訪問NSUserDefaults,並使用它填充您一直在詢問的「收藏夾」選項卡。

一點幫助的示例代碼(假設原始數據在UITableView中,就像您在之前的問題中所述)。我從頭到尾寫這篇文章(未經測試的代碼),所以你必須找出任何錯誤,但這個想法是正確的。

// in the .h file 
#import <UIKit/UIKit.h> 

@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> { 

    // set up a mutable array which allows editing of the array 
    NSMutableArray *myFavoritesData; 

} 
// set up a retained property 
@property (nonatomic, retain) NSMutableArray *myFavoritesData; 

@end 


// in the .m file 
// synthesize the getters/setters for your array 
@synthesize myFavoritesData; 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // find cell that was just pressed 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 

    // get pointer for the label that we want to identify the cell by 
    // the tag in this case is set to '5' in Interface Builder in the options for the label 
    UILabel *someLabel; 
    someLabel = (UILabel *)[cell viewWithTag:5]; 

    NSString *tmpFavorite = someLabel.text; 

    // get the count of the current array and use that for the "new" row since the count 
    // will always be 1 larger than the last object in the array (arrays start at 0, counts start at 1) 
    NSUInteger newRow = [self.myFavoritesData count]; 
    [self.myFavoritesData insertObject:tmpFavorite atIndex:newRow]; 

} 

// save the mutable array into NSUserDefaults when the view is about to disappear 
- (void) viewWillDisappear:(BOOL)animated 
{ 

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
    [userDefaults setObject:self.myFavoritesData forKey:@"MyFavorites"]; 

    // synchronize the data now instead of waiting for the OS to synchronize it at some 
    // arbitrary time in the future 
    [userDefaults synchronize]; 

} 

一旦你在新的視圖控制器,你可以從NSUserDefaults讀取和數組填充表。例如:

// favorites view controller 
- (void)viewDidLoad { 

    [super viewDidLoad]; 

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
    NSMutableArray *tmpArray = [[NSMutableArray alloc] init]; 
    tmpArray = [[userDefaults objectForKey:@"MyFavorites"] mutableCopy]; 

     if ([tmpArray count] == 0) { 

      // 
      // no favorites have ever been saved 
      // 


     } else { 

      // load the favorites into some array you synthesized just like before 
      self.tableFavoritesData = [[NSMutableArray alloc] init]; 
      self.tableFavoritesData = [[userDefaults objectForKey:@"MyFavorites"] mutableCopy]; 

      NSLog(@"favorites data is %d and %@", [self.tableFavoritesData count], self.tableFavoritesData); 

} 

[tmpArray release]; 
} 

然後在你的最愛cellForRowAtIndexPath視圖控制器,你只需訪問該陣列的每個索引每串(所以,串標0將進入零行,字符串索引1將進入第1行等),這是什麼填充你的收藏夾表!

試一下。

+0

非常感謝我得到了即時嘗試達到目標的結構... 即時通訊的一個小問題是我不想獲取單元格內容。我想獲取單元格顯示的視圖的內容。 我猜你需要更改此代碼 UILabel * someLabel; someLabel =(UILabel *)[cell viewWithTag:5]; NSString * tmpFavorite = someLabel.text; ? 我嘗試使用 (?[rootArray objectAtIndex:indexPath]設置] 但是它並沒有真正制定出 :( – 2010-08-17 09:33:03

+0

你怎麼會有'rootArray'定義。如果它被定義類似於我如何定義我上面的可變陣列,你會這樣做:'[self.rootArray objectAtIndex.indexPath.row];'其中'indexPath.row'是一個int並指定當前行按下 – iwasrobbed 2010-08-17 10:24:34

相關問題