开发者

Memory increases drastically when I type in a UITextfield

开发者 https://www.devze.com 2023-03-16 01:56 出处:网络
This is how I have created two textfields in a tableview and i release them in the dealloc method. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPa

This is how I have created two textfields in a tableview and i release them in the dealloc method.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    }

    cell.accessoryType = UITableViewCellAccessoryNone;

    if ([indexPath row] == 0) {
        text = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 300, 30)];
        text.delegate=self;
        text.userInteractionEnabled=NO;
        text.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
        if ( qNum-1 == 53 ) {
            text.placeholder=@"Category";
        }
        [cell.contentView addSubview:text];
    }
    if ([indexPath row] == 1) {
        text2 = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 300, 30)];
        text2.delegate=self;
        text2.userInteractionEnabled=NO;
        if(qNum-1==53) {
            text2.placeholder=@"Subcategory";
        }
        text2.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
        [cell.contentView addSubview:text2];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

The problem is as soon as the virtual keyboard appears and i start typing into the textfields the memory increases drastically. Can someone please help me out of this problem ? Thanks in advance.

Here is the code I have written inside the delegate methods

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;

}

- (void)textFieldDidBeginEditing:(UITextField *)textField{

if([text.text isEqualToString:@"United States"]||[text.text isEqualToString:@"Australia"]){
    text2.userInteractionEnabled=NO;
    item.title=@"State";
    myPickerView.hidden=YES;
    myPickerView2.hidden=NO;
    [actionSheet showInVi开发者_如何学Cew:mainView];
    [actionSheet setBounds:CGRectMake(0, 0, 320, 400)];
}

}


Your code has several problems. You are adding new UITextFields to your cell, even when reusing it, so you add new text fields over and over to the same cell. You need to build distinct setup logic for the different types of cells within the if (cell == nil) part, and only configure these cells later. Also, you probably want to release the text fields after adding them as a subview. Please refer to the examples in Apple's documentation for UITableView.

You might want to show your delegate methods - maybe there are some more problems.


try this

[cell.contentView addSubview:text];   
[text release];

and

[cell.contentView addSubview:text2];
[text2 release];


You are not releasing textfields


Are you using Instruments to detect memory allocation? I've noticed the same exact problem. Memory usage just shoots up when the keyboard shows up... I think it's something you don't have to worry about. There's nothing I did that fixed this, and it pretty much happens in every instance when the keyboard appears. Probably a bug of Instruments?

0

精彩评论

暂无评论...
验证码 换一张
取 消