Yet another UITableView Question
Posted
by
barbgal
on Stack Overflow
See other posts from Stack Overflow
or by barbgal
Published on 2011-02-16T04:56:08Z
Indexed on
2011/02/16
7:25 UTC
Read the original article
Hit count: 484
Hi,
I have a strange issue in my iPhone application. I have created a UITableView with 4 Sections and 3 Rows so totally 12 Rows. But
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
The above method only gets called for 9 times instead of 12 times.why this happenning. My 4th section is not getting constructed but my 1st section gets duplicated as 4th section.
Thanks for your time and help.
Plese refer my code below
@interface MainViewController : UITableViewController<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
{
}
@end
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
CGRect frameRect = CGRectMake(0,0,320,460);
UITableView *tableView = [[UITableView alloc] initWithFrame:frameRect
style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor purpleColor];
tableView.scrollEnabled = YES;
self.view = tableView;
[tableView release];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 3;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 4;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"CELL IS NIL %i", indexPath.section);
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.section == 0)
{
if(indexPath.row == 0)
{
cell.text = @"Tmail";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = @"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else if ( indexPath.row == 1 )
{
cell.text = @"English";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = @"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else
{
cell.text = @"Hindi";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = @"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
}
else if (indexPath.section == 1)
{
if(indexPath.row == 0)
{
cell.text = @"Street";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = @"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else if ( indexPath.row == 1 )
{
cell.text = @"City";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = @"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else
{
cell.text = @"State";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = @"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
}
else if (indexPath.section == 2)
{
if(indexPath.row == 0)
{
cell.text = @"Salem";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = @"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else if ( indexPath.row == 1 )
{
cell.text = @"Samalpatti";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = @"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else
{
cell.text = @"Chennai";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = @"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
}
else if (indexPath.section == 3)
{
if(indexPath.row == 0)
{
cell.text = @"NOKIA";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = @"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else if ( indexPath.row == 1)
{
cell.text = @"SAMSUNG";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = @"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
else
{
cell.text = @"SONY";
UITextField *aField = [[UITextField alloc]initWithFrame:CGRectMake(100,10,200,40)];
aField.placeholder = @"Mandatory";
aField.delegate = self;
aField.textColor = [UIColor blackColor];
[cell addSubview:aField];
[aField release];
}
}
}
return cell;
}
© Stack Overflow or respective owner