繁体   English   中英

IOS 7 UITextField resignFirstResponder BAD

[英]IOS 7 UITextField resignFirstResponder BAD

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

textField是一个全局变量。

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

我已经在Apple的帮助下成功修复了类似的崩溃错误。 关键是reuseIdentifer

报价来自Apple Developer Technical Support的 Vincent Gable发来的邮件:

这是在iOS 7中使用UITableView发生的已知行为更改,此时不重用单元格。

这里的解决方法是确保您遵循正确的单元重用。 如果您不想重复使用UITableViewCells ,那么建议您只需在UIScrollView布局所有视图。

要确保重用单元格,请确保将相同的字符串传递给dequeueReusableCellWithIdentifier:传递给reuseIdentifier:使用alloc/init创建单元格时。 这个字符串不能为零。

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

您需要对UITableViews如何工作以及重新考虑您的设计进行更多研究。 将UITextField存储在全局变量中并试图像这样定位它不是正确的方法。 即使您可以解决即时问题,这可能是UITextField与UITableViewCell一起发布的,但这种设计只会让您陷入困境。

相反,请考虑继承UITableViewCell并将UITextField属性添加到子类。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM