0

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

在可能的情况下如何查看表格视图?PS。即使我将 TaskRootController 切换到 TaskRootController : UITableViewController 行为也是一样的。我的代码如下:

AppDelegate.m

@implementation AppDelegate

@synthesize window = _window;
@synthesize taskController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];

    self.taskController = [TaskController alloc];
    self.window.rootViewController = self.taskController;

    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{

}

- (void)applicationDidEnterBackground:(UIApplication *)application
{

}

- (void)applicationWillEnterForeground:(UIApplication *)application
{

}

- (void)applicationDidBecomeActive:(UIApplication *)application
{

}

- (void)applicationWillTerminate:(UIApplication *)application
{

}

@end

任务控制器.m

@implementation TaskController

@synthesize taskRootController;

- (void) pushInboxController
{
    TaskBoxController *taskBoxController = [[TaskBoxController alloc] initWithNibName:nil bundle:NULL];
    [self pushViewController:taskBoxController animated:YES];
}

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.taskRootController = [[TaskRootController alloc] initWithNibName:nil bundle:NULL];
    UIViewController *root = self.taskRootController;
    [self initWithRootViewController: root];

}

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

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear: animated];
    [self performSelector:@selector(pushInboxController)];
}

@end

任务根控制器.m

@implementation TaskRootController

@synthesize taskRootView;

- (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(@"DUPA");

    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: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
    self.taskRootView.delegate = self;
    self.taskRootView.dataSource = self;
    [self.view addSubview:self.taskRootView];

}

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

- (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.
}

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

@end
4

4 回答 4

1

添加此委托方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = @"this is row";

    return cell;
}

编辑 :

init当您创建 in 对象TaskController时放入AppDelegate.m

self.taskController = [[TaskController alloc]init];

并将委托和数据源都放到TaskController.h

<UITableViewDataSource, UITableViewDelegate>

并添加其相关方法。

于 2013-03-21T04:57:10.247 回答
0

在你的 AppDelegate

     self.taskController = [[TaskController alloc]initWithNibName:@"TaskController" bundle:[NSBundle mainBundle]];
     UINavigationController *task = [[UINavigationController alloc] initWithRootViewController:self.taskController];

    self.window.rootViewController = task;

在您的任务控制器中

    - (void)viewDidLoad
       {
         [super viewDidLoad];
         TaskRootController *tc = [[TaskRootController alloc] initWithNibName:@"TaskRootController" bundle:[NSBundle mainbundle]];
        [self addChildViewController:tc];
        [tc didMoveToParentViewController:self];
        [self.view addSubview:tc.view];
       }
于 2013-03-21T05:11:10.960 回答
0

尝试这个 ::

设置 tableview 的委托和数据源

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [yourArray count];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil){
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    }
    cell.textLabel.text = @"row";
    return cell;
}

希望对你有帮助

于 2013-03-21T05:15:03.180 回答
0
in Appdelegate File :-

self.TaskViewController = [[TaskViewController alloc] initWithNibName:@"TaskViewController" bundle:nil];
self.navigationController=[[UINavigationController alloc]initWithRootViewController:self.TaskViewController];

[self.window setRootViewController:navigationController];//ios-6
                           or
[self.window addSubview:navigationController.view];//<ios-6

Add a Delegate Method in UITableViewDelagates:-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = @"Name";

    return cell;
}
于 2013-03-21T06:52:03.507 回答