I am trying to re-use cellViews using tags and Cell Identifiers, however the code below crashes whenever a cell is re-used. I think I’m almost there. Can anyone see the mistake?
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
const NSInteger BUTTON_TAG = 1001;
const NSInteger SWITCH_TAG = 1002;
const NSInteger TEXTFIELD_TAG = 1003;
NSString *CellIdentifier = @"";
if(indexPath.section == 2 && indexPath.row == 0)
CellIdentifier = @"Button";
else if (indexPath.section == 3)
CellIdentifier = @"Switch";
else
CellIdentifier = @"TextField";
UISwitch *switchView;
UITextField *textField;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (CellIdentifier == @"TextField")
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
CGRect frame = CGRectInset([cell.contentView bounds], 70, 10);
textField = [[[UITextField alloc] initWithFrame:frame] autorelease];
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.delegate = self;
cell.accessoryView = textField;
cell.tag = TEXTFIELD_TAG;
}
else if (CellIdentifier == @"Button")
{
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.clipsToBounds=YES;
cell.tag = BUTTON_TAG;
}
else if (CellIdentifier == @"Switch")
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
switchView = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
cell.accessoryView = switchView;
cell.tag = SWITCH_TAG;
}
}
else
{
textField = (UITextField*)[cell viewWithTag:TEXTFIELD_TAG];
switchView = (UISwitch*)[cell viewWithTag:SWITCH_TAG];
}
Crash Log
2012-02-22 14:50:08.352 ***[2304:207] -[UITableViewCell setSecureTextEntry:]: unrecognized selector sent to instance 0x6368270
2012-02-22 14:50:08.355 ***[2304:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell setSecureTextEntry:]: unrecognized selector sent to instance 0x6368270'
You don’t say what the crash is or provide a backtrace, but one problem I see right away is that you are always doing:
for all recycled cells even though only one of the three types has a switchView.
You also only set
TEXTFIELD_TAGfor one kind of cell as well, yet refer to it when accessing all types of “recycled” cells.EDITED TO ADD: I see you’ve added the exception from your console. The exception is being thrown on a call to
setSecureTextEntry. I don’t seesetSecureTextEntryanywhere in the code you copy & pasted into the question, so I’d suggest looking forsetSecureTextEntryin your real code and wherever it’s being called, make sure it’s aUITextFieldreceiving that call and not aUITableViewCell(which can be the super view in which a secureUITextFieldlives).