iphone UITableVIew(基于导航)
我正在开发一个基于导航的应用程序。 我的 rootViewController 包含 3 个单元格 -当按下第一个按钮时,将推送 UIViewController (这有效) -问题在于应该推送 UITableViewController 的第二个和第三个单元格
应用程序运行时没有错误,并且根本没有崩溃,但是当我导航到表视图时,会看到一个空表,其中没有任何元素。
这部分代码有问题吗? :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"Introduction" bundle:nil];
detailViewController.title=@"Introduction";
UITableViewController *myPictures = [[UITableViewController alloc] initWithNibName:@"Pictures" bundle:nil];
myPictures.title=@"Pictures";
UITableViewController *myImages = [[UITableViewController alloc] initWithNibName:@"ImagesViewController" bundle:nil];
myImages.title=@"Images";
// Pass the selected object to the new view controller.
if (0 == indexPath.row)
[self.navigationController pushViewController:detailViewController animated:YES];
if (1== indexPath.row)
[self.navigationController pushViewController:myPictures animated:YES];
if (2== indexPath.row)
[self.navigationController pushViewController:myImages animated:YES];
[detailViewController release];
[myPictures release];
[myImages release];
I'm working on a navigation-based application.
My rootViewController contains 3 cells
-When the first one is pressed a UIViewController is pushed (THIS WORKS)
-The problem is with the 2nd and 3rd cells that are supposed to push a UITableViewController
The app is running with no errors and it is NOT crashing at all, but when i navigate to the tableview, an empty table is viewed with no elements in it.
Is the problem with this part of the code?? :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"Introduction" bundle:nil];
detailViewController.title=@"Introduction";
UITableViewController *myPictures = [[UITableViewController alloc] initWithNibName:@"Pictures" bundle:nil];
myPictures.title=@"Pictures";
UITableViewController *myImages = [[UITableViewController alloc] initWithNibName:@"ImagesViewController" bundle:nil];
myImages.title=@"Images";
// Pass the selected object to the new view controller.
if (0 == indexPath.row)
[self.navigationController pushViewController:detailViewController animated:YES];
if (1== indexPath.row)
[self.navigationController pushViewController:myPictures animated:YES];
if (2== indexPath.row)
[self.navigationController pushViewController:myImages animated:YES];
[detailViewController release];
[myPictures release];
[myImages release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你做的事情非常错误(除了你原来的问题)。为什么要实例化每个视图控制器,然后仅使用基于当前“单元格选择”的视图控制器?这会减慢您的应用程序速度,具体取决于加载每个单独视图所需的时间。您应该只在“if (indexPath.row == 2) {”块内部实例化相关视图控制器。
除此之外,你的方法还有很多错误。你所做的永远不会起作用,因为实例化一个通用的 UITableViewController (即使你提供自己的笔尖)显然只会向你显示一个空视图。您需要有一个自己的类,笔尖作为委托绑定到该类,然后该委托将为 TableView 提供数据。
我相信当你创建这些nib文件(例如“Pictures”)时,xcode还会给你一个“PicturesViewController.h和PicturesViewController.m”文件?如果是这样,你需要在那里编写适当的代码并确保“Pictures”中的Tableview “ nib 文件将其“数据源”和“委托”设置为“PicturesViewController”。然后,当您想要显示该视图时,请执行以下操作:
What you're doing it very wrong (apart from your original problem). Why are you instantiating each view controller and then only using the one based on the current 'cell selection'? This will slow your app down depending on how much time it would take to load each of these separate views. You should only instantiate the relevant view controller inside the "if (indexPath.row == 2) {" block.
Apart from that there are many things wrong about your approach. What you are doing will never work since instantiating a generic UITableViewController (even if you supply your own nib) will obviously only show you an empty view. You need to have a class of your own which the nib is tied to as a delegate which will then supply the TableView with data.
I believe when you created these nib files (for example "Pictures") xcode also give you a 'PicturesViewController.h and PicturesViewController.m" files? If so, you need to write the appropriate code there and ensure the Tableview in the "Pictures" nib file has its 'datasource' and 'delegate' set to 'PicturesViewController'. Then when you want to show that view do this instead: