I'm trying to create and draw a UITableView populated by an array. I've created a UIViewController and set the UIViewController as my tableview's delegate. However, it seems that my cellForRowAtIndexPath method is not called when the tableview is created, so my table never gets populated. What am I missing here? (All labels are initialized in that function, as I'm just trying to create a Main Menu using a table, all entries will be fixed).
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
prefTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
[prefTable setDelegate:self];
//NSIndexSet *tmpIndRange = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0,1)];
//[prefTable insertSections:tmpIndRange withRowAnimation:UITableViewRowAnimationNone];
//[tmpIndRange release];
labelArray = [[NSMutableArray alloc] initWithCapacity:10];
[labelArray addObject:NSLocalizedString(@"Account ID",@"C_ACC_ID")];
[labelArray addObject:NSLocalizedString(@"Site ID",@"Site_ID")];
[labelArray addObject:NSLocalizedString(@"Station ID",@"Station_ID")];
[labelArray addObject:NSLocalizedString(@"Encryption Key",@"ENC_KEY")];
[labelArray addObject:NSLocalizedString(@"Encryption On",@"ENC_ON")];
placeholderArray = [[NSMutableArray alloc] initWithCapacity:10];
[labelArray addObject:NSLocalizedString(@"Required",@"Required")];
[labelArray addObject:NSLocalizedString(@"Required",@"Required")];
[labelArray addObject:NSLocalizedString(@"Required",@"Required")];
[labelArray addObject:NSLocalizedString(@"Required",@"Required")];
[labelArray addObject:NSLocalizedString(@"Required",@"Required")];
self.view = prefTable;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
NSInteger currenttag = textField.tag;
NSLog(@"%d",textField.tag);
//accountId = textField.text;
//stationId = textField.text;
//siteId = textField.text;
[textField resignFirstResponder];
return YES;
}
- (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] autorelease];
}
// Set up the cell...
[cell setAccessoryType:开发者_高级运维UITableViewCellAccessoryNone];
[cell.textLabel setText:[labelArray objectAtIndex:indexPath.row]];
//((TextInputTableCell *)cell).textField.placeholder = [placeholderArray objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
Also, will it be better if I assigned the menu items using a data source? It seems that section headers can only be assigned using a data source. Thanks!
tableView:cellForRowAtIndexPath:
is part of the UITableViewDataSource protocol - not UITableViewDelegate. You'll need to make your controller the datasource of the UITableView:
prefTable.dataSource = self;
精彩评论