问题
Im getting a crash, when using a UItextField, inside my customCell, and when i resignFirstResponder the textfield, but its not visible anymore(the table view scrolled out of window). I still can find the textfield, the pointer continues accessible, it is no null, and the crash only occurs on IOS7, on IOS6 i dont have this problem. Heres some code :
The textField
is a global variable.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[TableCell alloc] init];
if(indexPath.row == 0)
{
[textField setFrame:CGRectMake(15, 5, cell.frame.size.width-60, cell.frame.size.height)];
textField.textAlignment = NSTextAlignmentLeft;
[textField setBorderStyle:UITextBorderStyleNone];
textField.textColor = [UIColor blackColor];
textField.tag = indexPath.row;
textField.delegate = self;
textField.secureTextEntry = YES;
[textField setFont:[UIFont fontWithName:@"Arial-BoldMT" size:15]];
textField.textColor = [UIColor whiteColor];
textField.returnKeyType = UIReturnKeyDone;
[textField setAdjustsFontSizeToFitWidth:YES];
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Senha" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
[cell.contentView textField];
}
}
return cell;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
// NSLog(@"text field %@",textField);
// NSLog(@"tfield return: %d",textField.isFirstResponder);
[textField resignFirstResponder];
// [self.view endEditing:YES];
return NO;
}
回答1:
I've successfully fixed a similar crash bug with the help of Apple. The key is the reuseIdentifer
.
The quote is from a mail from Vincent Gable of Apple Developer Technical Support:
This is a known behavior change that happens in iOS 7 with
UITableView
, when cells are not reused.The fix here is to make sure that you follow proper cell reuse. If you do not want to re-use
UITableViewCells
, then it is recommended that you simply layout all your views inside aUIScrollView
.To make sure cells are re-used, make sure you are passing the same string to
dequeueReusableCellWithIdentifier:
that you pass toreuseIdentifier:
when usingalloc/init
to make the cell. This string can not be nil.
So I think you should make sure you've set TableCell
's reuseIdentifer
property with the same value you've passed to dequeueReusableCellWithIdentifier:
回答2:
You need to do some more research into how UITableViews work and reconsider your design. Storing a UITextField in a global variable and trying to position it like this is not the right approach. Even if you could solve the immediate problem, which is likely that the UITextField has been released along with the UITableViewCell, this design is only going to get you into trouble further down the line.
Instead, consider subclassing UITableViewCell and adding a UITextField property to your subclass.
You probably don't want to be using a different CellIdentifier for every single row either.
回答3:
Maybe i've solved. It's a little bit dirty methot but i think it work. I store all the cell that cellForRowAtIndexPath create
if (!cell)
{
cell = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"FormCell_%@",cellID] owner:nil options:nil] lastObject];
[self.allTheCell addObject:cell];
}
the app doesn't crash anymore on ios7
来源:https://stackoverflow.com/questions/19117589/ios-7-uitextfield-resignfirstresponder-bad