【问题标题】:View not visible视图不可见
【发布时间】:2026-01-23 22:15:01
【问题描述】:

我的应用程序从名为TaskController : UINavigationController 的根控制器开始 作为UINavigationController 的根视图控制器,我创建了类TaskRootController : UIViewController<UITableViewDelegate>(它已添加为视图UITableView); 当我启动应用程序时,我只看到任务根控制器的标题和背景颜色。 但我没有看到表格视图。 如果我的应用程序以TaskRootController 作为rootViewController 开头,我会看到表格视图。

在可能的情况下如何查看表格视图?

编辑

 @implementation TaskRootController

@synthesize taskRootView; //table view

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSLog(@"SIZE x:%f,y:%f ; %f:%f", self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height);
    self.view.backgroundColor = [UIColor grayColor];
    self.title = @"Root";
    self.taskRootView = [[UITableView alloc] initWithFrame: self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.taskRootView];
    self.taskRootView.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat result = 20.0f;
    if([tableView isEqual:self.taskRootView])
    {
        result = 40.0f;
    }
    return result;
}

@end

【问题讨论】:

  • 发布你的代码。你是如何设置 rootViewController 的?
  • @PratyushaTerli:回来+1!!!
  • 如果您已将表格视图放置在 .xib 中。然后确保将表视图委托和数据源添加到文件所有者。
  • 粘贴您编写的代码 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions mehtod
  • @user1863111-检查我的编辑:0

标签: iphone ios objective-c cocoa-touch uiview


【解决方案1】:

添加UITableView

将委托和数据源放入TaskRootController.h 文件中。

@interface TaskRootController : UIViewController <UITableViewDataSource, UITableViewDelegate>

并将这段代码放入TaskRootController.m文件中

self.tblView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
    self.tblView.delegate = self;
    self.tblView.dataSource = self;
    [self.view addSubview:self.tblView];

并添加UITabelView的相关delegate和datasource方法。

编辑:

检查您的以下方法..是否正确添加??

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1; // put number for section.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 6; // put number as you want row in section.
}

【讨论】:

  • 我认为这些方法不需要显示简单的表格视图(带有灰色虚线的白色背景),因为当我将 TaskRootController 设置为我的应用程序中的根控制器时(不是我的 UINavigationController 中的根),TaskRootController 会显示everythink ok )
【解决方案2】:

@iPatel - 我认为这些方法不需要显示简单的表格视图(带有灰色虚线的白色背景),因为当我将 TaskRootController 设置为我的应用程序中的根控制器时(不是我的 UINavigationController 中的根),TaskRootController 显示一切正常

@Manohar - 我手边没有代码。但我记得它看起来像:

self.taskRoot = [[TaskController alloc] initWithNill.....] //initialization of controller
self.window.rootViewController = self.taskRoot; 
[self.window makeKeyAndVisible];

【讨论】:

    最近更新 更多