iPhone SDK:应用程序在textField resignFirstResponder上崩溃

时间:2014-08-28 11:09:46

标签: ios iphone

我是一个创建应用程序,需要扩展tableview功能,如下面的屏幕。当我单击该行时,文本框和按钮将出现。当我单击文本框并按键盘返回时,应用程序崩溃。

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

请帮助。

1 个答案:

答案 0 :(得分:1)

您似乎正在使用正在取消分配的文本字段。我认为最好的方法是在每个单元格中添加文本字段,就像添加标签并将其设置为隐藏一样。在didSelectRow委托上,您应该将标签设置为隐藏,并且不隐藏文本字段。最好使用隐藏标志来从superview中删除和添加。

希望它有所帮助。