0

我知道我问了一个非常常见的问题。但是在经历了很多类似的问题和文章之后我无法理解

如何在 didSelectRowAtIndexPath 委托方法中访问 UITableViewCell 中的 UITextfield。

我在下面发布我的代码。我可能不会按照预期的方式进行,虽然..

我想在 didSelectRowAtIndexPath 中制作文本字段(选定行的)[becomeFirstResponder],以便我可以在此文本字段下方显示 uiPicker。这个你能帮我吗。

   - (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 方面,我想我有你的答案。

我在这里的另一个线程上找到了这个,并将其修改为更通用:

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

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

    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];

    UIView* 子视图 = [[cell.contentView 子视图] lastObject]; // 确保你的 UITextField 真的*是*你添加的最后一个对象。

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

    [文本字段成为FirstResponder];

}

...其中“textField”是实际文本字段对象的占位符。希望这可以帮助!

于 2012-05-08T23:45:30.993 回答
0

我认为您不需要分配初始化文本字段,因为它最终将指向单元格的文本字段(如果有)。

我相信你是在不必要地分配它。

于 2012-11-05T06:13:15.877 回答