我正在尝试使用标签和单元标识符来重新使用cellViews,但是当单元格被重新使用时,下面的代码会崩溃。我想我快到了。任何人都可以看到错误?当单元格被“回收”时,重用UITableView单元会导致崩溃
// 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];
}
崩溃日志
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'
取消注释// cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];'。没有? – 2012-02-22 14:38:04
为什么'cell = [[[UITableViewCell alloc] ...'注释掉了? – dasblinkenlight 2012-02-22 14:40:03
我已经取消注释了。只是测试中的错误 – JonWells 2012-02-22 14:40:37