I created a custom cell. When I use these custom cells in table view My cells getting replaced.I created about 10 sections in the table view in which each section contains 1 row. I am using Custom Cells for all 10 sections. When I scroll the view, the last 4 cells are replaced with top cells. I am using ReuseIdentifier but they are still getting replaced. Any Ideas to fix this? Thanks!
This is my CustomCell Code!
-(UITableViewCell *)returnCellForBirthdayDetails:(UITableView *)tableView
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellForBirthdayDetails"];
if(cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"CustomCellToAddDetails" owner:self options:nil];
cell = customCell;
// customCell = nil;
}
return cell;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[customCellTextField resignFirstResponder];
return YES;
}
- (UITextField *)textField
{
UITextField *textField = nil;
if( customCell )
{
textField = (UITextField *)[customCell.contentView viewWithTag:101];
}
return textField;
}
and Here I am calling custom cells in the tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
addCustomCell = [[CustomCellToAddDetailsController alloc] init];
switch (indexPath.section)
{
case 0:
cell = [addCustomCell returnCellForBirthdayDetails:tableView];
addCustomCell.customCellLabel.text = @"FirstName";
addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
addCustomCell.customCellTextField.placeholder = @"FirstName";
[delegate.fieldArray addObject:addCustomCell];
break;
case 1:
cell = [addCustomCell returnCellForBirthdayDetails:tableView];
addCustomCell.customCellLabel.text = @"LastName";
addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
addCustomCell.customCellTextField.placeholder = @"LastName";
[delegate.fieldArray addObject:addCustomCell];
break;
case 2:
cell = [addCustomCell returnCellForBirthdayDetails:tableView];
addCustomCell.customCellLabel.text = @"Dob";
addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
addCustomCell.customCellTextField.placeholder = @"Dob";
addCustomCell.customCellTextField.inputView = dp;
addCustomCell.customCellTextField.inputAccessoryView=toolBarForTableView;
[delegate.fieldArray addObject:addCustomCell];
break;
case 3:
cell = [addCustomCell returnCellForBirthdayDetails:tableView];
addCustomCell.customCellLabel.text = @"Address";
addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
addCustomCell.customCellTextField.placeholder = @"Enter Address";
[delegate.fieldArray addObject:addCustomCell];
break;
case 4:
cell = [addCustomCell returnCellForBirthdayDetails:tableView];
addCustomCell.customCellLabel.text = @"City";
addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
addCustomCell.customCellTextField.placeholder = @"Enter City";
[delegate.fieldArray addObject:addCustomCell];
break;
case 5:
cell = [addCustomCell returnCellForBirthdayDetails:tableView];
addCustomCell.customCellLabel.text = @"State";
addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
addCustomCell.customCellTextField.placeholder = @"Enter State";
[delegate.fieldArray addObject:addCustomCell];
break;
case 6:
cell = [addCustomCell returnCellForBirthdayDetails:tableView];
addCustomCell.customCellLabel.text = @"Email";
addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
addCustomCell.customCellTextField.placeholder = @"Enter Email";
[delegate.fieldArray addObject:addCustomCell];
break;
case 7:
cell = [addCustomCell returnCellForBirthdayDetails:tableView];
addCustomCell.customCellLabel.text = @"Phone";
addCustomCell.customCellLabel.font = [UIFont boldSystemFontOfSize:12];
addCustomCell.customCellTextField.placeholder = @"Enter MobileNo";
[delegate.fieldArray addObject:addCustomCell];
break;
}
return cell;
[addCustomCell release];
}
Please let me know what mistake I am making. Thanks!!
Use a different reuseIdentifier for each cell. When you use the same identifier, then the tableview will just grab any available cell with this identifier (in your case it’s grabbing the top ones since they are no longer in use).
Edit:
You can use the same identifier (and you should) for cells that are identical in everything except text/image. Then you can call a ‘configureCell’ method where you set the text/image/etc for each cell.