【问题标题】:Add to favorites function?添加收藏功能?
【发布时间】:2011-09-29 14:24:54
【问题描述】:

我在理解如何保存数据以便为我的应用程序添加“添加到收藏夹”功能时遇到了这个问题。 该应用程序有一个 UITableView 并且数据存储在一个 Plist 中。从那里 它进入一个包含 UIImageView 和 UITextView 的 DetailView。 我希望能够为我喜欢的项目添加书签并将它们显示在 单独的视图。

为了方便查看,这里有一段代码:

//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];
}

【问题讨论】:

  • 欢迎来到 SO!关于发布人们可以回答的问题的几个提示。明确你的问题是什么真的很有帮助,你想让人们更容易看到问题并做出回应。在这种特殊情况下,对于人们来说,这只是太多的代码,而且你还不清楚你尝试了什么以及它是如何失败的。将其精简到问题的核心部分,您会得到更好的答案。
  • 您要求 StackOverflow 用户为您的应用添加功能并为您编写代码。请阅读有关如何提问的建议,尤其是“做好功课”和“提出与其他用户相关的问题”部分。
  • 很抱歉,如果结果是这样,我只是想请教一下。

标签: iphone ios uitableview uinavigationcontroller


【解决方案1】:

您有几个实现收藏夹的选项之一(实际上主要取决于您的数据是否是持久的)。

  • (A) 您可以为表格中显示的每个项目添加一个标记, 将其标记为收藏 - 然后只需参考相同的数据集 但为所述标记过滤它。或者……

  • (B) 您可以创建一个附加列表,其中包含每个项目的副本 您想标记为收藏,然后将该新列表称为 您的数据源。

如果您的数据不是持久的,您仍然可以使用方法 A,并且在刷新数据时仅保留标记的记录,然后再插入新的新鲜数据。

希望这是有道理的!

【讨论】:

  • 谢谢,这让我更容易理解我必须做什么。我会试试看我能做些什么:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-19
相关资源
最近更新 更多