I am a complete newbie to iOS development but not to programming.
I am trying to create a simple application. I have opened Xcode 4.2 and created a new master view application. What I want to do is to set the contents of the table view in the master page.
I have edited my controller header file like this:
#import <UIKit/UIKit.h>
@interface MyAppMasterViewController : UITableViewController
{
NSArray *tableViewArray;
}
@property (nonatomic, retain) NSArray *tableViewArray;
@end
I have synthesized the tableViewArray variable in the controller implementation:
#import "MyAppMasterViewController.h"
@implementation MyAppMasterViewController
@synthesize tableViewArray;
And I have loaded an NSArray isntance into it in viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array = [[NSArray alloc] initWithObjects:@"Apple", @"Microsoft", @"HTC", nil];
self.tableViewArray = array;
}
How can I, now, assign this array (tableViewArray) to the table view?
How can I, now, assign this array (tableViewArray) to the table view?
You don't 'assign' an array to a table, you use some magic with delegates. Conform to the UITableViewDataSource
and UITableViewDelegate
in your .h like so:
@interface MyAppMasterViewController : UITableViewController <UITableViewDelegate,UITableViewDataSource>
Assign your class as the delegate (most likely in -viewDidLoad) for the table: then, your table view will query you for the all important -cellForRowAtIndexPath method, in which you set the title of the cell with something like this:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [_documentTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return ( cell );
}