0

私は非常に一般的な質問をしていることを知っています。しかし、同様の質問や記事をたくさん読んだ後、私は理解できません

didSelectRowAtIndexPath デリゲート メソッド内の UITableViewCell 内の UITextfield にアクセスするにはどうすればよいですか。

以下に私のコードを投稿しています。私は期待通りに行かないかもしれませんが..

このテキストフィールドの下にuiPickerを表示できるように、didSelectRowAtIndexPathで(選択された行の)テキストフィールドを[becomeFirstResponder]にしたいです。これで私を助けてください。

   - (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 *value=@"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];        
        }
    }
}
4

2 に答える 2

0

セルのサブビューを反復処理して UITextField を取得するという点では、答えがあると思います。

別のスレッドhereでこれを見つけ、より一般的なものに変更しました。

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

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

    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];

    UIView* subview = [[cell.contentView サブビュー] lastObject]; // UITextField が本当に最後に追加したオブジェクトであることを確認してください。

    if ([サブビュー isKindOfClass:[UITextField クラス]]) {
        textField = (UITextField*)サブビュー;
    }

    [textField becomeFirstResponder];

}

...ここで、「textField」は実際のテキスト フィールド オブジェクトのプレースホルダーです。お役に立てれば!

于 2012-05-08T23:45:30.993 に答える
0

テキストフィールドを初期化する必要はないと思います。最終的にはセルのテキストフィールド(存在する場合)を指すためです。

不必要に割り当てていると思います。

于 2012-11-05T06:13:15.877 に答える