简体   繁体   English

当我输入 UITextfield 时,Memory 急剧增加

[英]Memory increases drastically when I type in a UITextfield

This is how I have created two textfields in a tableview and i release them in the dealloc method.这就是我在 tableview 中创建两个文本字段并在 dealloc 方法中释放它们的方式。

- (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.问题是,一旦虚拟键盘出现并且我开始在文本字段中输入,memory 就会急剧增加。 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 showInView: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.您正在将新的UITextFields添加到您的单元格,即使在重用它时,您也会一遍又一遍地向同一个单元格添加新的文本字段。 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.您需要if (cell == nil)部分中为不同类型的单元格构建不同的设置逻辑,并且稍后再配置这些单元格。 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 .请参阅 Apple 文档中有关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?你是用 Instruments 检测 memory 的分配吗? 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.当键盘出现时,Memory 的使用量会迅速增加......我认为这是你不必担心的事情。 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?可能是仪器的错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM