2014-08-28 50 views
0

我創建的應用程序需要擴展如下面的屏幕視圖功能。當我點擊行時,文本框和按鈕將出現。當我點擊文本框,然後按鍵盤返回時,應用程序崩潰。iPhone SDK:應用程序崩潰在textField resignFirstResponder

enter image description here

當我試圖辭職截圖顯示的文本框中,應用程序崩潰, 我寫下面的代碼...

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    return 10; 

} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AffiliationCell"]; 
    cell.backgroundColor = [UIColor clearColor]; 

    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 130, 44)]; 
    lblTitle.tag = indexPath.row+1; 
    lblTitle.font = [UIFont fontWithName:Oxygen_Regular size:13]; 
    lblTitle.text = [NSString stringWithFormat:@"Affiliation %d",indexPath.row+1]; 
    lblTitle.backgroundColor = [UIColor clearColor]; 
    lblTitle.textColor = [UIColor blackColor]; 

    [cell.contentView addSubview:lblTitle]; 



    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell* checkCell = [tableView cellForRowAtIndexPath:indexPath]; 
    [tableView beginUpdates]; 
    if ([indexPath compare:self.checkedIndexPath] == NSOrderedSame) { 
     self.checkedIndexPath = nil; 

     [viewScreenName removeFromSuperview]; 

    } else { 

     //add view 
     [txtScreenName setClearsOnInsertion:YES]; 
     viewScreenName.frame = CGRectMake(0, 45, viewScreenName.frame.size.width, viewScreenName.frame.size.height); 
     [checkCell.contentView addSubview:viewScreenName]; 
     self.checkedIndexPath = indexPath; 
    } 

    [tableView endUpdates]; 



} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if ([indexPath compare:self.checkedIndexPath] == NSOrderedSame) { 

     return expandedCellHeight; // Expanded height 
    } 
    return 44 ; 
} 


#pragma mark - TextField Delegate 

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ 

    tblAffiliations.frame = updatedFrame; 
    return TRUE; 
} 
- (BOOL)textFieldShouldReturn:(UITextField *)textField{ 
    @try { 
     if (textField) { 
      [textField resignFirstResponder]; 

     } 

    } 
    @catch (NSException *exception) { 
     NSLog(@"Exception %@",exception); 
    } 
    tblAffiliations.frame = currentFrame; 
    return TRUE; 
} 

請幫助。

+2

當我們的應用程序崩潰時,您可以發佈控制檯中顯示的堆棧跟蹤嗎? – 2014-08-28 11:11:17

+0

使用' - (BOOL)textFieldShouldEndEditing:(UITextField *)textField' – Mohit 2014-08-28 11:15:40

回答

1

看來您正在使用正在釋放的文本字段。我認爲最好的處理方式是在每個單元格中添加文本框,就像添加標籤並將其設置爲隱藏一樣。在didSelectRow委託上,您應該將標籤設置爲隱藏並且文本字段不隱藏。最好使用從超級視圖中刪除和添加的隱藏標誌。

希望它有幫助。

+0

感謝您的解決方案爲我工作。 – 2014-09-02 07:07:32