タグとセルIDを使用してcellViewを再利用しようとしていますが、セルを再利用すると以下のコードがクラッシュします。私はほとんどそこにいると思う。誰もが間違いを見ることができますか?コメント細胞のallocラインの横にセルを再利用すると、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'
コメントを外します。//セル[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; '。いいえ? –
なぜ 'cell [[[UITableViewCell alloc] ... 'がコメントアウトされましたか? – dasblinkenlight
私はコメントを外しました。テストの間違い – JonWells