制作iphone收藏夹标签

时间:2010-08-16 20:48:45

标签: iphone

如何制作一个iphone选项卡,从数组加载所选数据并根据用户选择进行更改。

1 个答案:

答案 0 :(得分: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的字符串将进入第0行,索引1的字符串将进入第1行,等等,这就是填充你最喜欢的桌子的东西!

尝试一下。