Hi everyone,
I am a newbie to all things Objective-C and iPhone/iPad programming, and this is my first post on this forum. Hopefully you guys will be able to help me. I am trying to create an UITableView control and display some data on it. I've been reading Apple's documentation for it for almost two days now (not full time, of course) and still can't create one. I know there are various approaches, but I chose the one where you don't create a UITableViewController, but instead derive a class from UIiewController and have it adopt the UITableViewDataSource and UITableViewDelegate protocols. However, there seems to be something missing, since I only get an empty Table View when I run the program. I will post my code here:
Here is the code for my main App Delegate's .h file:
And it's .m file (I only include this method because it is the only one I modified):
According to the docs, loadView is supposed to be called automatically, but debugging the program, it seems that it wasn't getting called, so I manually added it. Didn't seem to have any effect, though. I commented it out later.
Finally, here is the code for the class which acts as the delegate/data source.
The header file:
And the .m file (again, I've included only those methods overriden, or those necessary for the protocols):
From a programmming point of view, everything seems to be set up correctly, but nothing happens when I run the program. I'm thinking that the problem might in the way my xib file is set up. In it, I have a reference to a MyTVDelegate as the File's Owner, with its view outlet set to a reference of a UITableView object present in the same nib file. Its datasource and delegate outlets are unconnected. The project is based on the "View-based Application" template.
If you guys can help me determine what is wrong here, I would be eternally grateful. Many thanks for any info.
I am a newbie to all things Objective-C and iPhone/iPad programming, and this is my first post on this forum. Hopefully you guys will be able to help me. I am trying to create an UITableView control and display some data on it. I've been reading Apple's documentation for it for almost two days now (not full time, of course) and still can't create one. I know there are various approaches, but I chose the one where you don't create a UITableViewController, but instead derive a class from UIiewController and have it adopt the UITableViewDataSource and UITableViewDelegate protocols. However, there seems to be something missing, since I only get an empty Table View when I run the program. I will post my code here:
Here is the code for my main App Delegate's .h file:
Code:
#import <UIKit/UIKit.h>
#import "MyTVDelegate.h"
@class TableTestViewController;
@interface TableTestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
//TableTestViewController *viewController;
MyTVDelegate *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MyTVDelegate *viewController;
@end
And it's .m file (I only include this method because it is the only one I modified):
Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[viewController loadView];
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
According to the docs, loadView is supposed to be called automatically, but debugging the program, it seems that it wasn't getting called, so I manually added it. Didn't seem to have any effect, though. I commented it out later.
Finally, here is the code for the class which acts as the delegate/data source.
The header file:
Code:
#import <Foundation/Foundation.h>
@interface MyTVDelegate : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *items;
}
@property(nonatomic,retain) NSMutableArray *items;
@property(nonatomic,retain) IBOutlet UITableView *tView;
@end
And the .m file (again, I've included only those methods overriden, or those necessary for the protocols):
Code:
#import "MyTVDelegate.h"
@implementation MyTVDelegate
@synthesize items;
@synthesize tView;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath * )indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
NSString *texto = [items objectAtIndex:indexPath.row];
cell.textLabel.text = texto;
return cell;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
-(void) loadView
{
[items addObject:@"Item 1"];
[items addObject:@"Item 2"];
[items addObject:@"Item 3"];
UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
tableView.autoresizingMask =UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
tableView.delegate =self;
tableView.dataSource = self;
self.view = tableView;
//[tableView release];
}
@end
From a programmming point of view, everything seems to be set up correctly, but nothing happens when I run the program. I'm thinking that the problem might in the way my xib file is set up. In it, I have a reference to a MyTVDelegate as the File's Owner, with its view outlet set to a reference of a UITableView object present in the same nib file. Its datasource and delegate outlets are unconnected. The project is based on the "View-based Application" template.
If you guys can help me determine what is wrong here, I would be eternally grateful. Many thanks for any info.