I got a prototype table with a custom Cell and inside this cell a textField.
My array of cells is a large one, so when I scroll the table, the cells need to be recreated.
Testing, when I scroll the text that was in the txt field of 1 cell goes to another, keyboard types change and everything gets messed-up!
CODE:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"customTableCell";
customTableViewCell *cell = (customTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[customTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
}
// Configuration
cell.lblName.text = [self.pfFields objectAtIndex: [indexPath row]];
cell.txtType = [self.pfTypes objectAtIndex: [indexPath row]];
cell.mySql = [self.pfSql objectAtIndex: [indexPath row]];
//cell.txtField.delegate = self;
if ([[self.pfTypes objectAtIndex:[indexPath row]] isEqualToString: @"n"]) {
[cell.txtField setKeyboardType:UIKeyboardTypeNumberPad];
} else if ([[self.pfTypes objectAtIndex:[indexPath row]] isEqualToString: @"m"]) {
[cell.txtField setKeyboardType:UIKeyboardTypeEmailAddress];
}
return cell;
}
You need to be sure to set all properties of a cell in tableView:cellForRowAtIndexPath:
, because a given cell can be reused at any time by the controller.
To prevent the text from jumping around, you need to set cell.txtField.text
to the appropriate NSString
loaded from your cell's backing object. Similarly, you should always set the keyboardType
every time (I assume that there are cases besides just n
and m
). Setting the properties every time ensures that you get the correct display regardless of how the reused cell was previously configured.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.