制作iphone收藏夹选项卡
如何制作一个iphone选项卡,从阵列加载选定数据并根据用户选择进行更改。Alx 一个简单的场景是创建一个基于NIB的制作iphone收藏夹选项卡,iphone,Iphone,如何制作一个iphone选项卡,从阵列加载选定数据并根据用户选择进行更改。Alx 一个简单的场景是创建一个基于NIB的UITableViewCell(有很多关于这个的教程),上面有某种标签 然后,您可以在用户选择单元格时获取标签的内容,并将其存储到可变数组中,然后将该数组存储到NSUserDefaults中 然后,您可以从另一个视图访问NSUserDefaults,并使用它填充您一直询问的“收藏夹”选项卡 有一点示例代码可以帮助您(假设原始数据位于UITableView中,如您在前面的问题中所述
UITableViewCell
(有很多关于这个的教程),上面有某种标签
然后,您可以在用户选择单元格时获取标签的内容,并将其存储到可变数组中,然后将该数组存储到NSUserDefaults
中
然后,您可以从另一个视图访问NSUserDefaults
,并使用它填充您一直询问的“收藏夹”选项卡
有一点示例代码可以帮助您(假设原始数据位于UITableView中,如您在前面的问题中所述)。我在脑子里写下了这个(未经测试的代码),所以你必须找出任何bug,但这个想法是正确的
// 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];
}
然后在favorites视图控制器的cellForRowAtIndexPath
中,您只需访问数组每个索引中的每个字符串(因此,索引0的字符串将进入第0行,索引1的字符串将进入第1行,等等),这就是填充favorites表的内容
试试看。这是可能的。不过,你的问题太模糊了。非常感谢,我已经了解了我现在想要表达的内容的结构。。。不过我有一个小问题,就是我不想得到单元格的内容。我想获取单元格显示的视图内容。我猜您需要更改此代码UILabel*someLabel;someLabel=(UILabel*)[带标记的单元格视图:5];NSString*tmpFavorite=someLabel.text?我尝试使用([[rootArray objectAtIndex:indexPath]集];但是它没有真正起作用:(您如何定义
rootArray
呢?如果它的定义与我在上面定义可变数组的方式类似,您可以这样做:[self.rootArray ObjectAtIndeXpath.row];
其中indepath.row
是一个int,指定当前按下的行
// 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];
}