Pankti Patel
Pankti Patel

Reputation: 178

iPhone SDK: App crash on textField resignFirstResponder

I an creating app that needs expand tableview feature like below screen. when i click the row the textbox and button will appear.when i click on textbox and press return from keyboard, application crashed.

enter image description here

When I tried to resign the text box showing in screenshot, application crashed, I write Following Code...

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

Please Help.

Upvotes: 0

Views: 430

Answers (1)

David Bemerguy
David Bemerguy

Reputation: 1364

It seems that you are using a textfield that is being deallocated. I think the best way to proceed is adding the textfield in each cell like you added your label and setting it to be hidden. On the didSelectRow delegate, you should set the label as hidden and the textfield not hidden. It is better to work with hidden flag that to remove and add from superview.

Wish it helps.

Upvotes: 1

Related Questions