循环遍历 didSelectRowAtIndexPath 中自定义单元格的子视图以获取 UITextField 文本



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

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

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

我想在didSelectRowAtIndexPath中使文本字段(选定行)[成为第一响应者]以便我可以在此文本字段下方显示 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];        
        }
    }
}

在遍历单元格子视图和获取 UITextField 方面,我想我有你的答案。

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    UITextField* textField = [[UITextField alloc] init];    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];    UIView* subview = [[cell.contentView subviews] lastObject];确保你的UITextField确实是你添加的最后一个对象。    if ([subview isKindOfClass:[UITextField class]]) {        文本字段 = (UITextField*)子视图;    }    [文本字段成为第一响应者];}

。其中"textField"是实际文本字段对象的占位符。希望这有帮助!

我认为你不需要分配init文本字段,因为它最终会指向单元格的文本字段(如果有的话)。

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

最新更新