2010-09-16 75 views
1

你好未来的朋友谁是会帮我很大的时间在这个项目上,XML解析的数据为自定义视图用的UILabel而不是UITableView的

我有这些书从XML文件解析(从这个职位,我发现通过)。第一个视图(RootViewController)有一个UITable中书名的列表。当用户点击其中一本书,而不是在第二个UITable中查看书籍详细信息(BooksDetailViewController)时,我希望标题,作者和摘要在UILabels和UITextView中的界面生成器中放置在自定义视图中。

IBOutlet UILabel *bookTitle; 
IBOutlet UILabel *bookAuthor; 
IBOutlet UITextView *bookSummary; 

我相信我的问题与RootViewController.m“didSelectRowAtIndexPath”有关。如果我理解了我已适当调整的示例(我不太确定),它会将Book数组传递到我的BooksDetailViewController上新表的每一行。

Book *aBook = [appDelegate.books objectAtIndex:indexPath.row]; 

我已经尝试了几件事重新创建didSelectRowAtIndexPath,但我没有太多的运气。下面我将示例代码注释掉,并用我自己的可怕猜测代码/叹息。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

     // if(bdvController == nil) 
     // bdvController = [[BookDetailViewController alloc] initWithNibName:@"BookDetailView" bundle:[NSBundle mainBundle]]; 
     // Book *aBook = [appDelegate.books objectAtIndex:indexPath.row]; 
     // bdvController.aBook = aBook; 
     // [self.navigationController pushViewController:bdvController animated:YES]; 


     NewBookDetailViewController *detailViewController = [[NewBookDetailViewController alloc] initWithNibName:@"NewBookDetailViewController" bundle:nil]; 
     Book *aBook = appDelegate.books; 

     //detailViewController.aBook.title = bookTitle.text; 
     //detailViewController.aBook.author = bookAuthor.text; 
     //detailViewController.aBook.summary = bookSummary.text; 

     [self.navigationController pushViewController:detailViewController animated:YES]; 
     [detailViewController release]; 
} 

我是不是真的被剖析数据aBook.title在RootViewController的的didSelectRowAtIndexPath方法连接到BOOKTITLE(的UILabel)?

难道我连他们的viewDidLoad在NewBookDetailViewController.m?

bookTitle.text = aBook.title; 
bookAuthor.text = aBook.author; 
bookSummary.text = aBook.summary; 

什么是在RootViewController中为自定义视图而不是表视图编写didSelectRowAtIndexPath的正确方法?

请对我轻松点。

  • CLO

回答

0

NewBookDetailViewController的目的是显示一本图书的信息,是吗?所以NewBookDetailViewController将需要一种被告知应该显示哪本书的方式。

这样做的一个简单方法是将一个属性添加到这个类中,以保存它应该显示的Book的引用。您不想将整个图书阵列传递给详细视图控制器。它应该如何知道要展示哪本书?

这里是我的实现是什么样子:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    Book *selectedBook = [appDelegate.books objectAtIndex:indexPath.row]; 
    NewBookDetailViewController* viewController = [[NewBookDetailViewControlleralloc] initWithNibName:@"NewBookDetailViewController"]; 
    viewController.book = selectedBook; // "book" is a property you'd add to NewBookDetailViewController 

    [self.navigationController pushViewController:viewController animated:YES]; 
    [viewController release]; 
} 

这里是你如何添加book属性NewDetailViewController

.h文件:

@class Book; 
@interface NewDetailViewController : UIViewController { 
    // other instance variables 
    Book *_book; 
} 

@property (nonatomic, retain) Book *book; 

@end 

.m文件:

#import "NewDetailViewController.h" 
#import "Book.h" // assuming the "Book" class is defined in Book.h 

@implementation NewDetailViewController 

@synthesize book = _book; 

- (void)dealloc { 
    [_book release]; 
    [super dealloc]; 
} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    // set up your controls to display the book information 
} 

// other method implementations 

@end 
+0

感谢亚历克斯如此快速的回复。我现在要尝试你的建议。在你回复之前,我实际上会添加一条评论...... - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath :(NSIndexPath *)indexPath {*} {* * * * * * * * * * * * * * * * * * * * * * * * * detailViewController.bookTitle.text = aBook.title; detailViewController.bookAuthor.text = aBook.author; detailViewController.bookSummary.text = aBook.summary; [self.navigationController pushViewController:detailViewController animated:YES]; [detailViewController release]; } – slowman21 2010-09-16 16:51:08

+0

DUDE!你太棒了!我想我明白了!非常感谢。 – slowman21 2010-09-16 17:01:17

相关问题