IOS 7 UITextField resignFirstResponder BAD

我得到一个崩溃,当使用一个UItextField,在我的customCell,当我resignFirstResponder文本字段,但它不再可见(表视图滚动窗口)。 我仍然可以find文本字段,指针可以继续访问,它不是空的,崩溃只发生在IOS7 IOS6我没有这个问题。 下面是一些代码:

textField是一个全局variables。

 - (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; } 

我已经在苹果的帮助下成功解决了类似的崩溃问题。 关键是reuseIdentifer

引用来自苹果开发者技术支持 Vincent Gable的邮件:

这是iOS 7中使用UITableView发生的已知行为变化,当单元格不被重用时。

这里的解决方法是确保您遵循适当的单元重用。 如果你不想重新使用UITableViewCells ,那么build议你简单地在UIScrollView布置所有的视图。

为了确保单元格被重用,请确保将相同的string传递给dequeueReusableCellWithIdentifier:您传递给reuseIdentifier:使用alloc/init来创build单元格时。 这个string不能为零。

所以我认为你应该确保你已经将TableCellreuseIdentifer属性设置为与你传递给dequeueReusableCellWithIdentifier:值相同的值dequeueReusableCellWithIdentifier:

您需要对UITableView的工作方式进行更多的研究,并重新考虑您的devise。 将UITextField存储到一个全局variables中并尝试像这样定位不是正确的方法。 即使你能解决眼前的问题,UITextField可能与UITableViewCell一起被释放,但这种devise只会让你陷入困境。

相反,考虑UITableViewCell的子类和UITextField属性添加到您的子类。

您可能不希望为每一行使用不同的CellIdentifier。

也许我已经解决了。 这有点脏,但我认为它的工作。 我存储cellForRowAtIndexPath创build的所有单元格

if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"FormCell_%@",cellID] owner:nil options:nil] lastObject]; [self.allTheCell addObject:cell]; } if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"FormCell_%@",cellID] owner:nil options:nil] lastObject]; [self.allTheCell addObject:cell]; }应用程序不会在ios7上崩溃