0

i know im asking a very common question. but after going through a lots of similar questions and articles im not able understand

How can i access UITextfield inside UITableViewCell inside didSelectRowAtIndexPath delegate method.

im posting my code below. i may b not going with expected way although..

i want to make textfield (of selected row) [becomeFirstResponder] in didSelectRowAtIndexPath so that i can display uiPicker below to this textfield. Please help me out with this.

   - (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];        
        }
    }
}
Sangeeta
  • 164
  • 4
  • 16

2 Answers2

0

In terms of iterating through the cell subviews and getting the UITextField, I think I have your answer.

I found this on another thread here, and modified it to be more generic:

- (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];

}

...where "textField" is the placeholder for your actual text field object. Hope this helps!

Community
  • 1
  • 1
0

I don't think you need to alloc init the textfield, for it will ultimately be pointing to the cell's textfield (if any).

I believe you are allocating it unnecessarily.

Julian B.
  • 3,605
  • 2
  • 29
  • 38