tags:

views:

72

answers:

1

I am using code like this

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tv { return 3; }

  • (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section { if(section==2) return 20; else return 1;

}

  • (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    //if (cell == nil) cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"]autorelease];

    // Configure the cell switch (indexPath.section) { case 0: { textField=[[[UITextField alloc]initWithFrame:CGRectMake(5, 10, 290, 70)]autorelease]; textField.delegate=self; textField.keyboardType=UIKeyboardTypeURL; textField.autocorrectionType=YES; textField.textColor=[UIColor blackColor]; textField.placeholder=@"Enter feed url"; [cell.contentView addSubview:textField]; break; } case 1: { textField1=[[[UITextField alloc]initWithFrame:CGRectMake(5, 10, 290, 70)]autorelease]; textField1.delegate=self; textField1.keyboardType=UIKeyboardTypeURL; textField1.textColor=[UIColor blackColor]; textField1.autocorrectionType=YES; textField1.placeholder=@"Enter starting url"; [cell.contentView addSubview:textField1]; break; } case 2: { cell.text=[[PopeularSiteArray objectAtIndex:indexPath.row]objectForKey:@"Title"]; break; } default : break;

    } return cell; }

When i scrolling my tableview the textfield should alloc every time the delegate function called...i changed that code like when the textfield is nothing only it willbe created but that time it shows an garbage values in textfield

What could i use here Any one help me? Thanks in advance

A: 

You should always keep a reference on the controls you add to UITableView cells: UITextFiled, UISwitch, etc. By this way you can interact with the control as well as get/set the value more easily. Otherwise you must find the right cell with cellForRowAtIndexPath and try to get the child controls.

To solve your problem:

  1. Add textFieldFeedURL and textFieldStartingURL as class members (in your .h file).
  2. Allocate ONCE these members in viewDidLoad or cellForRowAtIndexPath
    if (textFieldFeedURL != nil) {
    textFieldFeedURL = [[UITextField alloc] initWithFrame:CGRectMake(5, 10, 290, 70)];
    }
  3. Add the text fields to the cells in cellForRowAtIndexPath with addSubView.
  4. Release the text fields in dealloc.

If you app has tens of controls, you may retain a reference in a NSMutableArray.

I hope I well understood your question. Good luck!

rjobidon