Iphone 添加到收藏夹功能?
我在理解如何保存数据以使我的应用程序具有“添加到收藏夹”功能时遇到了这个问题。 该应用程序具有UITableView,数据存储在Plist中。从那里 它进入包含UIImageView和UITextView的DetailView。 我希望能够将我喜欢的项目添加到书签中,并将它们显示在 另见 下面是一段代码,使其更容易查看:Iphone 添加到收藏夹功能?,iphone,ios,uitableview,uinavigationcontroller,Iphone,Ios,Uitableview,Uinavigationcontroller,我在理解如何保存数据以使我的应用程序具有“添加到收藏夹”功能时遇到了这个问题。 该应用程序具有UITableView,数据存储在Plist中。从那里 它进入包含UIImageView和UITextView的DetailView。 我希望能够将我喜欢的项目添加到书签中,并将它们显示在 另见 下面是一段代码,使其更容易查看: //BooksLibraryDao.h #import <Foundation/Foundation.h> @interface BooksLibraryDa
//BooksLibraryDao.h
#import <Foundation/Foundation.h>
@interface BooksLibraryDao : NSObject {
NSString *libraryPlist;
NSArray *libraryContent;
}
@property (nonatomic, readonly) NSString *libraryPlist;
@property (nonatomic, readonly) NSArray *libraryContent;
- (id)initWithLibraryName:(NSString *)libraryName;
- (NSDictionary *)libraryItemAtIndex:(int)index;
- (int)libraryCount;
@end
//BooksLibraryDao.m
#import "BooksLibraryDao.h"
@implementation BooksLibraryDao
@synthesize libraryContent, libraryPlist;
- (id)initWithLibraryName:(NSString *)libraryName {
if (self = [super init]) {
libraryPlist = libraryName;
libraryContent = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:libraryPlist ofType:@"plist"]];
}
return self;
}
- (NSDictionary *)libraryItemAtIndex:(int)index {
return (libraryContent != nil && [libraryContent count] > 0 && index < [libraryContent count])
? [libraryContent objectAtIndex:index]
: nil;
}
- (int)libraryCount {
return (libraryContent != nil) ? [libraryContent count] : 0;
}
- (void) dealloc {
if (libraryContent) [libraryContent release];
[super dealloc];
}
@end
//BooksTableViewController.h
#import <UIKit/UIKit.h>
#import "BooksLibraryDao.h"
#import "BooksListingViewCell.h"
#import "BooksAppDelegate.h"
@interface BooksTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *booksTableView;
BooksLibraryDao *dao;
IBOutlet BooksListingViewCell *_cell;
}
@end
//BooksTableViewController.m
#import "BooksTableViewController.h"
#import "DetailViewController.h"
#import "BooksListingViewCell.h"
#import "BooksNavController.h"
@implementation BooksTableViewController
#define CELL_HEIGHT 70.0
#pragma mark -
#pragma mark Initialization
/*
- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
self = [super initWithStyle:style];
if (self) {
// Custom initialization.
}
return self;
}
*/
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.backgroundColor = [UIColor clearColor];
}
- (void)viewWillAppear:(BOOL)animated {
dao = [[BooksLibraryDao alloc] initWithLibraryName:@"TestData"];
self.title = @"Books";
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [dao libraryCount];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"LibraryListingCell";
BooksListingViewCell *cell = (BooksListingViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"BooksListingView" owner:self options:nil];
cell = [_cell autorelease];
_cell = nil;
}
cell.titleLabel.text = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"title"];
cell.smallImageView.image = [UIImage imageNamed:[[dao libraryItemAtIndex:indexPath.row] valueForKey:@"smallImage"]];
cell.backgroundColor = [UIColor colorWithRed:9 green:9 blue:9 alpha:.7];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor colorWithRed:.1 green:.1 blue:.1 alpha:1];
cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease];
UIImage *selectionBackground;
selectionBackground = [UIImage imageNamed:@"cell.png"];
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *controller = [[DetailViewController alloc]
initWithBookData:[dao libraryItemAtIndex:indexPath.row]
nibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
controller.title = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"title"];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return CELL_HEIGHT;
}
#pragma mark -
#pragma mark Table view delegate
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
//DetailViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#import <QuartzCore/QuartzCore.h>
@interface DetailViewController : UIViewController <MFMailComposeViewControllerDelegate>{
IBOutlet UIImageView *bookImageView;
IBOutlet UILabel *titleLabel;
IBOutlet UITextView *authorTextView;
IBOutlet UITextView *descriptionTextView;
IBOutlet UILabel *message;
NSDictionary *bookData;
}
@property (nonatomic, retain) UIImageView *bookImageView;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UITextView *descriptionTextView;
@property (nonatomic, retain) UITextView *authorTextView;
@property (nonatomic, retain) IBOutlet UILabel *message;
-(IBAction)showPicker:(id)sender;
-(void)displayComposerSheet;
-(void)launchMailAppOnDevice;
-(IBAction)showAuthor;
-(IBAction)showDesc;
-(IBAction)showImage;
- (id)initWithBookData:(NSDictionary *)data nibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
@end
//DetailViewController.m
#import "DetailViewController.h"
@implementation DetailViewController
@synthesize bookImageView, titleLabel, descriptionTextView, authorTextView;
@synthesize message;
- (id)initWithBookData:(NSDictionary *)data nibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
bookData = data;
}
return self;
}
- (void)viewDidLoad {
bookImageView.image = [UIImage imageNamed:[bookData valueForKey:@"bookImage"]];
titleLabel.text = [bookData valueForKey:@"title"];
descriptionTextView.text = [bookData valueForKey:@"description"];
authorTextView.text = [bookData valueForKey:@"author"];
[super viewDidLoad];
}
//BooksLibraryDao.h
#进口
@接口BookLibraryDAO:NSObject{
NSString*libraryPlist;
NSArray*图书馆内容;
}
@属性(非原子,只读)NSString*libraryPlist;
@属性(非原子,只读)NSArray*libraryContent;
-(id)initWithLibraryName:(NSString*)libraryName;
-(NSDictionary*)图书馆项目索引:(int)索引;
-(int)图书馆计数;
@结束
//图书馆
#导入“BooksLibraryDao.h”
@图书馆dao的实现
@综合图书馆内容、图书馆内容;
-(id)initWithLibraryName:(NSString*)libraryName{
if(self=[super init]){
libraryPlist=libraryName;
libraryContent=[[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:类型为@“plist”]]的libraryPlist;
}
回归自我;
}
-(NSDictionary*)图书馆项目索引:(int)索引{
return(libraryContent!=nil&&[libraryContent count]>0&&index<[libraryContent count])
?[图书馆内容对象索引:索引]
:无;
}
-(内部)图书馆计数{
返回(libraryContent!=nil)【libraryContent计数】:0;
}
-(无效)解除锁定{
if(图书馆内容)[图书馆内容发布];
[super dealoc];
}
@结束
//BooksTableViewController.h
#进口
#导入“BooksLibraryDao.h”
#导入“BookListingViewCell.h”
#导入“BooksAppDelegate.h”
@接口BookTableViewController:UITableViewController{
ibuitableview*booksTableView;
图书馆道*道;
IBOutlet BookListingViewCell*\单元;
}
@结束
//BooksTableViewController.m
#导入“BooksTableViewController.h”
#导入“DetailViewController.h”
#导入“BookListingViewCell.h”
#导入“BooksNavController.h”
@TableViewController的实现
#定义单元高度70.0
#布拉格标记-
#杂注标记初始化
/*
-(id)initWithStyle:(UITableViewStyle)样式{
//Override initWithStyle:如果以编程方式创建控制器并希望执行不适合viewDidLoad的自定义。
self=[super initWithStyle:style];
如果(自我){
//自定义初始化。
}
回归自我;
}
*/
#布拉格标记-
#pragma标记视图生命周期
-(无效)viewDidLoad{
[超级视图下载];
self.tableView.backgroundColor=[UIColor clearColor];
}
-(无效)视图将显示:(BOOL)动画{
dao=[[BookLibraryDAO alloc]initWithLibraryName:@“TestData”];
self.title=@“图书”;
[self.tableView取消浏览索引路径:[self.tableView indexPathForSelectedRow]已设置动画:是];
}
#布拉格标记-
#pragma标记表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
//返回节数。
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回[dao libraryCount];
}
//自定义表格视图单元格的外观。
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“LibraryListingCell”;
BookListingViewCell*单元格=(BookListingViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
[[NSBundle mainBundle]loadNibNamed:@“BooksListingView”所有者:自选项:nil];
细胞=[[细胞自动释放];
_细胞=无;
}
cell.titleLabel.text=[[dao libraryItemAtIndex:indexath.row]valueForKey:@“title”];
cell.smallImageView.image=[UIImage ImageName:[[dao libraryItemAtIndex:indexath.row]valueForKey:@“smallImage”];
cell.backgroundColor=[UIColor COLOR WITHRED:9绿色:9蓝色:9 alpha:.7];
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.textLabel.textColor=[UIColor colorWithRed:.1绿色:.1蓝色:.1阿尔法:1];
cell.selectedBackgroundView=[[[UIImageView alloc]init]autorelease];
UIImage*选择背景;
selectionBackground=[UIImage ImageName:@“cell.png”];
((UIImageView*)单元格。selectedBackgroundView)。image=selectionBackground;
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
DetailViewController*控制器=[[DetailViewController alloc]
initWithBookData:[dao libraryItemAtIndex:indexPath.row]
nibName:@“DetailViewController”绑定:[NSBundle mainBundle]];
controller.title=[[dao libraryItemAtIndex:indexath.row]valueForKey:@“title”];
[self.navigationController pushViewController:控制器已设置动画:是];
[控制器释放];
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径{
返回单元高度;
}
#布拉格标记-
#pragma标记表视图委托
#布拉格标记-
#pragma标记内存管理
-(无效)未收到记忆警告{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//放弃未使用的任何缓存数据、图像等的所有权。
}
-(无效)视图卸载{
//放弃可在viewDidLoad或按需重新创建的任何内容的所有权。
//例如:self.myOutlet=nil;
}
-(无效)解除锁定{
[super dealoc];
}
@结束
//DetailViewController.h
#进口
#进口
#进口
#进口
@界面详细信息ViewController:UIViewController{
IBUIImageView*bookImageView;
IBUILabel*标题标签;
IBOutlet UITextView*authorTextView;
IBOutlet UITextView*descriptionTextView;
IBUILabel*消息;
NSDictionary*图书数据;
}
@p