2012-04-25 3 views
0

私は非常によくある質問をしています。同様の質問や記事がたくさんありましたが、私は理解できませんでした。UITextFieldテキストを取得するためにdidSelectRowAtIndexPathのカスタムセルのサブビューをループする

didItelectRowAtIndexPathデリゲートメソッド内のUITableViewCell内のUITextfieldにどうやってアクセスできますか?

私のコードを以下に掲載しています。私はこのテキストフィールドに下にuiPickerを表示することができるようdidSelectRowAtIndexPath の[becomeFirstResponder](選択された行の)テキストフィールドを作りたい

..ものの期待方法一緒に行くNOT Bできます。これで私を助けてください。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 


     if(self.isTableForHospital) 
     { 
      UIView *myview=[self createRowViewForHospitalTable:[customArray objectAtIndex:indexPath.row]]; 
      myview.tag=indexPath.row; 
      [cell addSubview:myview]; 
      cell.selectionStyle=UITableViewCellSelectionStyleNone; 
     } 
     else 
     { 
      cell.textLabel.text=[customArray objectAtIndex:indexPath.row]; 
     } 
    } 
    return cell; 
} 

-(UIView *)createRowViewForHospitalTable:(NSString *)rowText 
{ 
    UIView *hospital_row_view=[[UIView alloc]init]; 
    UILabel *lblRowText=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 50)]; 
    lblRowText.text=rowText; 

    txtDepartment=[[UITextField alloc]initWithFrame:CGRectMake(310, 0, 185, 30)]; 
     //...rest styling for textfield goes here.. 
    txtRole=[[UITextField alloc]initWithFrame:CGRectMake(495, 0, 185, 30)]; 

    [hospital_row_view addSubview:lblRowText]; 
    [hospital_row_view addSubview:txtDepartment]; 
    [hospital_row_view addSubview:txtRole]; 
    return hospital_row_view; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField { } 

- (void)textFieldFinished:(id)sender{ 
    NSString *[email protected]"sa"; 
    [sender resignFirstResponder]; 
} 

// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    UITableViewCell *cellSelected = [tableView cellForRowAtIndexPath: indexPath]; 

    UIView *myview=[cellSelected.contentView viewWithTag:indexPath.row]; 
    for(UIView *item in [myview subviews]) 
    { 
     if([item isKindOfClass:[UITextField class]]) 
     { 
      [item becomeFirstResponder];   
     } 
    } 
} 

答えて

0

セルのサブビューを繰り返し、UITextFieldを取得するという点では、あなたの答えがあると思います。

私は別のスレッド hereでこれを発見し、より汎用的にそれを修正:「テキストフィールド」は、あなたの実際のテキストフィールドオブジェクトのプレースホルダです

 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITextField* textField = [[UITextField alloc] init]; 

    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; 

    UIView* subview = [[cell.contentView subviews] lastObject]; // Make sure your UITextField really *is* the last object you added. 

    if ([subview isKindOfClass:[UITextField class]]) { 
     textField = (UITextField*)subview; 
    } 

    [textField becomeFirstResponder]; 

} 

...。 これが役立つことを願っています!

0

私はあなたが最終的にセルのテキストフィールド(もしあれば)を指しているので、テキストフィールドを初期化する必要はないと思います。

私はあなたが不必要にそれを割り当てていると思います。