使用 UITableView 而不是 UIPickerView 来设置 UITextField 文本

Posted

技术标签:

【中文标题】使用 UITableView 而不是 UIPickerView 来设置 UITextField 文本【英文标题】:Use UITableView instead of UIPickerView to set UITextField text 【发布时间】:2013-12-17 00:01:45 【问题描述】:

我想使用UITextFieldUITextField 中输入文本,而不是使用UITextField .tag 的UIPickerView

视图相当复杂,由我将解释的几个视图组成。

htmlContainerScrollView, contains multiple
 - axisContainerScrollView, contains multiple
  - itemField

UITableView - used to pass text into itemField

考虑到这一点,这就是我设置 htmlContainerScrollView 的方式。我添加了 cmets 来解释我想要实现的目标。

- (void) displayViews 
    // add scrollview, This view is ued to hold all of the axis (vertical scrolling only)
    htmlContainerScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 44.0, self.view.frame.size.width, 309.0)];

    //TODO: replace this array with a dynamic implementation of the axis images. Will need someone to design the images for me
    imagesArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"LPA.png"], [UIImage imageNamed:@"LPB.png"], [UIImage imageNamed:@"LPC.png"], [UIImage imageNamed:@"LPD.png"], [UIImage imageNamed:@"LPA.png"], [UIImage imageNamed:@"LPB.png"], [UIImage imageNamed:@"LPC.png"], [UIImage imageNamed:@"LPD.png"], [UIImage imageNamed:@"LPA.png"], [UIImage imageNamed:@"LPB.png"], [UIImage imageNamed:@"LPC.png"], [UIImage imageNamed:@"LPD.png"], [UIImage imageNamed:@"LPA.png"], [UIImage imageNamed:@"LPB.png"], [UIImage imageNamed:@"LPC.png"], [UIImage imageNamed:@"LPD.png"], [UIImage imageNamed:@"LPA.png"], [UIImage imageNamed:@"LPB.png"], [UIImage imageNamed:@"LPC.png"], [UIImage imageNamed:@"LPD.png"], [UIImage imageNamed:@"LPA.png"], [UIImage imageNamed:@"LPB.png"], [UIImage imageNamed:@"LPC.png"], [UIImage imageNamed:@"LPD.png"], nil];

    // Loop creates each axis
    tagNumber = 1; // init the tagNumver starting from 1
    for(int i = 0; i< axisNo; i++) 
        CGFloat y = i * 77; // places each axis 77pxl below the previous

        int itemsForRow = [itemsArray[i] intValue]; // get the current number of items for this axis
        int scrollWidth = (itemsForRow * 40)+4; // calculate scroll width using the umber of items for this axis * by the size of each item.textfield

        // create axis scroll view (horizontal scrolling only)
        UIScrollView *axisContainerScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, y,self.view.frame.size.width, 77.0)]; //device view width etc.
        axisContainerScrollView.contentSize = CGSizeMake(scrollWidth, 77.0); // axis.view scrollable width
        axisContainerScrollView.backgroundColor = [UIColor whiteColor];
        [htmlContainerScrollView addSubview:axisContainerScrollView];

        // make sure the view goes right to the edge of the screen.
        UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(0.0, 0.0, scrollWidth+10, 77.0);


        //grey header for each cell, if the total number of items exceeds the width of the device view then make the header the correct size
        if(scrollWidth > self.view.frame.size.width) 
            topBorder = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, scrollWidth, 18.0)];
            topBorder.backgroundColor = [UIColor colorWithRed:colorController.fbRed/255.0 green:colorController.fbGreen/255.0 blue:colorController.fbBlue/255.0 alpha:1.0];
         else 
            topBorder = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 18.0)];
            topBorder.backgroundColor = [UIColor colorWithRed:colorController.fbRed/255.0 green:colorController.fbGreen/255.0 blue:colorController.fbBlue/255.0 alpha:1.0];
        

        [axisContainerScrollView addSubview:topBorder];
        [axisContainerScrollView addSubview:view];

        int itemsCount = [itemsArray[i] intValue];


        // add axis image to each scrollview (i.e. a, b, c, d, e, f, g etc.)
        UIImageView *currentAxisImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, y, 18.0, 18.0)];
        currentAxisImage.image = imagesArray[i];
        [htmlContainerScrollView insertSubview:currentAxisImage aboveSubview:view];


        // loop creates each item for current axis
        for (int i = 0; i < itemsCount; i++) 
            // create header for itemField
            itemHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake((i*40)+2, 20, 40, 15)];
            itemHeaderLabel.backgroundColor = [UIColor colorWithRed:colorController.grRed/255.0 green:colorController.grGreen/255.0 blue:colorController.grBlue/255.0 alpha:1.0];
            [itemHeaderLabel setTextAlignment:NSTextAlignmentCenter];
            itemHeaderLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
            itemHeaderLabel.textColor = [UIColor whiteColor];
            NSString *strFromInt = [NSString stringWithFormat:@"%d",i+1]; // start from 1 not 0
            itemHeaderLabel.text = strFromInt;
            [view addSubview:itemHeaderLabel];

            // itemField is the UITextField I would like to add text too from UITableViewCell selections
            itemField = [[UITextField alloc] initWithFrame:CGRectMake((i*40)+2, 35, 40, 40)];
            itemField.delegate = self; // set delegate so you can use UITextField delegate methods
            itemField.textColor = [UIColor blackColor];
            [itemField setTextAlignment:NSTextAlignmentCenter];
            itemField.font = [UIFont fontWithName:@"Helvetica" size:20];
            itemField.backgroundColor=[UIColor whiteColor];
            itemField.layer.borderColor = [[UIColor colorWithRed:colorController.grRed/255.0 green:colorController.grGreen/255.0 blue:colorController.grBlue/255.0 alpha:1.0] CGColor];
            itemField.layer.borderWidth = 0.5f;
            [view addSubview:itemField];

            itemField.tag = tagNumber; // set tag
            tagNumber ++;

            [columnArrayOfTextFields addObject:itemField]; // array of items textfields.. not using this currently but might become usefull in the future. (legacey code)
        
        [rowArrayOfTextFields addObject:columnArrayOfTextFields]; // array of arrays.. not using this currently but might become usefull in the future. (legacey code)

    

    // exit loop, set first reasponder to the first itemField.
    [itemField viewWithTag:1];
    [itemField becomeFirstResponder];

    // add the whole scrollview to the mainview.
    htmlContainerScrollView.contentSize = CGSizeMake(self.view.frame.size.width, 77 *axisNo);
    [self.view addSubview:htmlContainerScrollView];

所以此时我已经创建了视图并为每个 itemField 分配了一个唯一的标签值。现在我将向您展示我的didSelectRowAtIndexPath 不完整,我认为这是我应该使用UITextFieldDelegates 设置文本的地方,但我不确定如何。

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

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    itemField.text = selectedCell.textLabel.text; // dose not work.. dosnt call UITextfield delegates or anything

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; //turns the UITableViewCell button as apposed to a single press fielf 

最后这些是我的 UITextFieldDelegates。

- (void)textFieldDidBeginEditing:(UITextField *)textField 

    itemField.text = textField.text;


-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
    return NO;  // Hide keyboard so that you can use the UITableView selections to populate the UITextfeild itemField


-(BOOL)textFieldShouldReturn:(UITextField*)textField;

    currentSelected.text = textField.text;
    NSInteger nextTag = currentSelected.tag + 1;
    // Set next responder
    UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
    if (nextResponder) 
        // Found next responder, so set it.
        [nextResponder becomeFirstResponder];
     else 
        // probably dont need this if I am not showing the UIkeyboard
        [textField resignFirstResponder];
    
    return NO; // We do not want UITextField to insert line-breaks.

【问题讨论】:

【参考方案1】:

您应该创建一个自定义表格视图单元格,其中包含一个 uitextfield。这样您就可以在 cellForRowAtIndexPath 中轻松地将其委托设置为 self,如下所示:

- (UITableViewCell *)tableView:(UITableView *)view cellForRowAtIndexPath:(NSIndexPath *)indexPath

     MyCellWithTextField* cell = .. //create cell
     cell.textField.delegate = self;
     return cell;

如果你不想把它放在 tableview 中,修复这个:

[itemField viewWithTag:1];
[itemField becomeFirstResponder]; 

应该是:

itemField = [view viewWithTag:1];
[itemField becomeFirstResponder]; 

【讨论】:

我尝试了第二种解决方案,但不幸的是它没有工作。我想将其保留为我创建的滚动视图,因此仍在尝试以这种方式对其进行排序。

以上是关于使用 UITableView 而不是 UIPickerView 来设置 UITextField 文本的主要内容,如果未能解决你的问题,请参考以下文章

使用 Restkit 上传图像 - 向 UITableView 添加两行而不是一行

RxSwift MVVM 实现查询

UIScrollView+UIView 而不是 UITableView+customcells?

在 UITableView 中拖放整个部分而不是单行

UIViewController 中的 UIButton 而不是 UITableView 的问题

更喜欢 UITableView 的重新排序控制手势而不是自定义平移手势