使用RootController的多视图TableView(Multi

编程入门 行业动态 更新时间:2024-10-21 18:34:11
使用RootController的多视图TableView(Multi-View TableView with RootController)

我正在尝试使用导航模板创建一个多视图应用程序。 我希望桌子位于初始视图的底部,顶部有其他视图(图像视图,标签等)。

最初我修改了RootViewController.xib以调整tableview的大小,并添加了图像视图,但除了全尺寸表外没有显示任何内容。

所以我修改了RootViewController.xib来添加UIView,然后在该视图中添加了一个UITableView和UIImageView。 我为tableView添加了IBOutlet。 在我的RootViewController.xib中,File'S Owner(RootViewController)与视图和表视图有出口连接。 当我右键单击UITableView时,我看到文件所有者的引用插座。 当我右键单击UIView时,我看到文件所有者的引用插座。 (我没有UIImageView的出口,因为我不修改代码;我需要一个?)。

但是,当我启动应用程序时,它会崩溃并显示以下消息:

'NSInternalInconsistencyException', 原因:' - [UITableViewController loadView]加载了“RootViewController”笔尖,但没有得到UITableView。

这是cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *QuizIdentifier = @"QuizIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:QuizIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:QuizIdentifier] autorelease]; } // Configure the cell. UIImage *_image = [UIImage imageNamed:@"icon"]; cell.imageView.image = _image; NSInteger row = [indexPath row]; cell.textLabel.text = [_categories objectAtIndex:row]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; }

I am trying to create a multi-view app with navigation template. I want table on the bottom part of initial view, with other views (image view, labels, etc) on the top.

Originally I modified RootViewController.xib to resize tableview, and added image view, but nothing displayed except full-size table.

So I modified RootViewController.xib to add UIView, then added a UITableView and UIImageView to that view. I added IBOutlet for the tableView. In my RootViewController.xib, File'S Owner (RootViewController) has outlet connection to the view and the table view. When I right-click the UITableView, I see the referencing outlet to File's Owner. When I right-click the UIView, I see the referencing outlet to File's Owner. (I don't have an outlet to UIImageView, since I don't modify in code; do I need one?).

However, when I launch the app, it crashes with message:

'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "RootViewController" nib but didn't get a UITableView.'

Here is the cellForRowAtIndexPath method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *QuizIdentifier = @"QuizIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:QuizIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:QuizIdentifier] autorelease]; } // Configure the cell. UIImage *_image = [UIImage imageNamed:@"icon"]; cell.imageView.image = _image; NSInteger row = [indexPath row]; cell.textLabel.text = [_categories objectAtIndex:row]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; }

最满意答案

你得到了这个错误,因为当你创建一个基于导航的iOS应用程序时,Xcode使RootViewController成为UITableViewController的子类。 将UITableView与普通UIView ,您的子类不再符合UITableViewController类。 因此,在RootViewController.m和.h文件中将UITableViewController更改为UIViewController 。 您可能还需要在init方法中更改其他一些内容,但让我们从这开始。

尝试这个:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"\n\nI'm in Root / cellForRowAtIndexPath"); static NSString *QuizIdentifier = @"QuizIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:QuizIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:QuizIdentifier] autorelease]; UIImage *_image = [UIImage imageNamed:@"icon"]; cell.imageView.image = _image; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } // Configure the cell. NSInteger row = [indexPath row]; cell.textLabel.text = [_categories objectAtIndex:row]; NSLog(@" We are at row %i with label %@ ", row, cell.textLabel.text); return cell; }

You're getting that error because when you create a navigation-based iOS app, Xcode makes the RootViewController a subclass of UITableViewController. Once you combine that UITableView with a normal UIView, your subclass no longer complies with the UITableViewController class. So, in your RootViewController.m and .h files change UITableViewController to UIViewController. You may also need to change a few other things in your init method, but let's just start with this.

Try this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"\n\nI'm in Root / cellForRowAtIndexPath"); static NSString *QuizIdentifier = @"QuizIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:QuizIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:QuizIdentifier] autorelease]; UIImage *_image = [UIImage imageNamed:@"icon"]; cell.imageView.image = _image; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } // Configure the cell. NSInteger row = [indexPath row]; cell.textLabel.text = [_categories objectAtIndex:row]; NSLog(@" We are at row %i with label %@ ", row, cell.textLabel.text); return cell; }

更多推荐

本文发布于:2023-08-04 23:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424573.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   RootController   Multi   TableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!