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

时间:2013-12-17 00:01:45

标签: ios uitableview uiscrollview uitextfield uitextfielddelegate

我想使用UITextField使用UITextField .tag

将文字输入UIPickerView而不是UITextField

视图非常复杂,包含几个视图,我将解释。

htmlContainerScrollView, contains multiple
 - axisContainerScrollView, contains multiple
  - itemField

UITableView - used to pass text into itemField

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

- (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 个答案:

答案 0 :(得分:0)

您应该创建一个自定义的tableview单元格,其中包含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];