2009-05-28 3 views
0

나는이델리게이트 함수가 호출 될 때마다 어떻게 텍스트 필드를 만들 수 있습니까? 내가 위임 함수가 호출 때마다 ALLOC해야 내 tableview에 텍스트 필드를 스크롤 할 때

- (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]; 
      [email protected]"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; 
      [email protected]"Enter starting url"; 
      [cell.contentView addSubview:textField1]; 
      break; 
     } 
     case 2: 
     { 
      cell.text=[[PopeularSiteArray objectAtIndex:indexPath.row]objectForKey:@"Title"]; 
      break; 
     } 
     default : 
      break; 

    } 
    return cell; 
} 

과 같은 코드를 사용하고 ... 나는 텍스트 필드 만이 생성됩니다 아무 것도 없을 때처럼 그 코드를 변경하지만를 텍스트 필드에 가비지 값을 표시합니다.

여기서 무엇을 사용할 수 있습니까?

답변

0

UITableView 셀에 추가하는 컨트롤 (UITextFiled, UISwitch 등)에 대한 참조는 항상 유지해야합니다. 이렇게하면 컨트롤과 상호 작용할 수있을뿐만 아니라 값을 쉽게 가져 오거나 설정할 수 있습니다. 그렇지 않으면 cellForRowAtIndexPath를 사용하여 올바른 셀을 찾아서 자식 컨트롤을 가져와야합니다.

  1. 이 (당신의 .H 파일) 클래스 멤버로 textFieldFeedURL 및 textFieldStartingURL을 추가

    가 문제를 해결합니다.
  2. 한번 할당이있는 viewDidLoad에서 회원 또는 cellForRowAtIndexPath는
    if (textFieldFeedURL != nil) { 
    textFieldFeedURL = [[UITextField alloc] initWithFrame:CGRectMake(5, 10, 290, 70)]; 
    }
  3. 는 addSubView로 cellForRowAtIndexPath에서 셀에 텍스트 필드를 추가합니다.
  4. dealloc의 텍스트 필드를 해제하십시오.

앱에 수십 개의 컨트롤이있는 경우 NSMutableArray에 참조를 유지할 수 있습니다.

귀하의 질문을 잘 이해하시기 바랍니다. 행운을 빕니다!

관련 문제