我创建了一个UITableView,有4个部分和3行,总共12行。但是
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
The above method only gets called for 9 times instead of 12 times. Why this happening? My 4th section is not getting constructed but my 1st section gets duplicated as 4th section.
请参阅下面的代码
@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;
}