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

发布于 2024-09-19 22:47:10 字数 2397 浏览 3 评论 0原文

你好,未来的朋友们,他们会在这个项目上帮助我,

我有这些从 XML 文件解析的书籍(从我发现的这篇文章中采用)。第一个视图 (RootViewController) 在 UITable 中有一个书名列表。当用户单击其中一本书时,我希望标题、作者和摘要位于带有 UILabels 和 UITextView 的 Interface Builder 中布局的自定义视图中,而不是在第二个 UITable 中查看书籍详细信息 (BooksDetailViewController)。

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 的正确方法是什么?

请对我放轻松一点。

  • 克洛

Hello future friends who is gonna help me big time on this project,

I have these Books parsed from XML file (adopted from this post I found). First view (RootViewController) has a list of book titles in a UITable. When user clicks on one of the books, instead of viewing the books detail (BooksDetailViewController) in a second UITable, I would like the Title, Author and Summary to be in a custom view laid out in Interface Builder with UILabels and UITextView.

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

I believe my problem has to do with RootViewController.m "didSelectRowAtIndexPath". If I understand the example I had adapted properly (which I am not confident about), it passed the Book array into each row of the new table on my BooksDetailViewController. 

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

I have tried a few things to recreate  didSelectRowAtIndexPath, but I am not having much luck. Below I have the example code commented out and with my own terrible guessed code /sigh.

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

Do I actually connect the parsed data aBook.title to bookTitle (UILabel) in didSelectRowAtIndexPath of RootViewController? 

or 

Do I connect them in viewDidLoad in NewBookDetailViewController.m?

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

What is the proper way to write the didSelectRowAtIndexPath in RootViewController for a custom view instead of a table view?

Please take it easy on me.

  • Clo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2024-09-26 22:47:10

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

The purpose of NewBookDetailViewController is to display information about a single book, yes? So NewBookDetailViewController will need a way of being told which book it's supposed to display.

An easy way of doing this would be to add a property to this class to hold a reference to the Book it's supposed to display. You would not want to pass the entire array of books to the detail view controller. How is it supposed to know which single book to display?

Here' how my implementation would look:

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

Here's how you add the book property to NewDetailViewController:

The .h file:

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

@property (nonatomic, retain) Book *book;

@end

The .m file:

#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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文