簡體   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