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 showInView:mainView];
[actionSheet setBounds:CGRectMake(0, 0, 320, 400)];
}
}
Your code has several problems. You are adding new
UITextFieldsto 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 theif (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 forUITableView.You might want to show your delegate methods – maybe there are some more problems.