简体   繁体   English

使用UITableView代替UIPickerView设置UITextField文本

[英]Use UITableView instead of UIPickerView to set UITextField text

I would like to use a UITextField to enter text into a UITextField instead of UIPickerView using the UITextField .tag 我想用一个UITextField文本输入到一个UITextField ,而不是UIPickerView使用UITextField .TAG

The view is quite complex and consists of several views which I will explain. 该视图非常复杂,由几个视图组成,我将对其进行解释。

htmlContainerScrollView, contains multiple
 - axisContainerScrollView, contains multiple
  - itemField

UITableView - used to pass text into itemField

So with that in mind, this is how I set up my htmlContainerScrollView . 因此,考虑到这一点,这就是我设置htmlContainerScrollView的方式 I have added comments to explaine what I am trying to achive. 我添加了评论以解释我要达到的目标。

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

So at this point I have created the view and assigned each itemField a unique tag value. 因此,在这一点上,我已经创建了视图,并为每个itemField分配了唯一的标记值。 Now I will show you my didSelectRowAtIndexPath which is not complete, I think this is where I should set the text using the UITextFieldDelegates but I am not sure how. 现在,我将向您展示我的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 
}

and finally these are my UITextFieldDelegates. 最后是我的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.
}

You should create a custom tableview cell which has a uitextfield in it. 您应该创建一个包含uitextfield的自定义tableview单元格。 This way you can easily set its delegate to self in cellForRowAtIndexPath like this: 这样,您可以像这样在cellForRowAtIndexPath中轻松地将其委托设置为self:

- (UITableViewCell *)tableView:(UITableView *)view cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     MyCellWithTextField* cell = .. //create cell
     cell.textField.delegate = self;
     return cell;
}

If you don't want to put it in tableview, fix this: 如果您不想将其放在表格视图中,请解决此问题:

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

should be: 应该:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM