XML使用UILabel而不是UITableView将数据解析为自定义视图

时间:2010-09-16 16:05:06

标签: iphone xml uilabel uitableview

你好未来的朋友,他们将帮助我在这个项目上大放异彩,

我从XML文件解析了这些书籍(从我发现的这篇文章中采用)。第一个视图(RootViewController)有一个UITable中的书名列表。当用户点击其中一本书,而不是在第二个UITable中查看书籍详细信息(BooksDetailViewController)时,我希望标题,作者和摘要位于Interface Builder中使用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)?

我是否在NewBookDetailViewController.m中的viewDidLoad中连接它们?

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

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

请放轻松我。

  • CLO

1 个答案:

答案 0 :(得分: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