2009-05-28 8 views
0

私は私のテーブルビューをスクロールテキストフィールドは、デリゲート関数が呼び出されるたびにALLOC必要があります...私は、テキストフィールドが何もない場合にのみ、それが作成されるようにそのコードを変更するが、そのとき、私はこのデリゲート関数が呼び出されるたびにテキストフィールドの割り当てを行うにはどうすればよいですか?

- (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中の細胞にテキストフィールドを追加しますONCE割り当てます。
  4. deallocのテキストフィールドを解放します。

アプリに何十ものコントロールがある場合、NSMutableArrayにリファレンスを保持できます。

私はあなたの質問をよく理解していただきたいと思います。がんばろう!

関連する問題