I am trying to create 3 table view cells using code (w/o nib). I am having some trouble getting the code to work. I guess I am not getting the approach right. Can anyone advise me on the proper way going forward? Any help on this will be greatly appreciated!
Thanks!
Zhen Hoe
My code snippet as follows:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 3;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
int row = [indexPath row];
UITableViewCell *startCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UITableViewCell *durationCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UITableViewCell *radiusCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
startCell.textLabel.text = @"Start:";
durationCell.textLabel.text = @"Duration:";
radiusCell.textLabel.text = @"radius";
if (row == 0)
{
return startCell;
}
else if (row == 1)
{
return durationCell;
}
return radiusCell;
}
EDIT (19/05/11)
After going through your answers, I am still unable to display any cells in my tableview. Is this due to the way I initialized my table?
//Initialization
UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.settingsView.frame.size.height)
style:UITableViewStyleGrouped];
self.tableView = tv;
[self.view addSubview:tableView];
After which I have an animation to expand the tableView
[UIView animateWithDuration:0.3 animations:^{
[self.tableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.picker.frame.size.height)];
}];
Do you guys see any issues with the above? Anything that is causing my display of cells fail?
Thanks!
Zhen