2014-08-28 55 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