Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用UITableView而不是UIPickerView来设置UITextField文本_Ios_Uitableview_Uiscrollview_Uitextfield_Uitextfielddelegate - Fatal编程技术网

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

Ios 使用UITableView而不是UIPickerView来设置UITextField文本,ios,uitableview,uiscrollview,uitextfield,uitextfielddelegate,Ios,Uitableview,Uiscrollview,Uitextfield,Uitextfielddelegate,我想使用UITextField将文本输入UITextField,而不是使用UITextField.tag的UIPickerView 这个观点相当复杂,由几个观点组成,我将对此进行解释 htmlContainerScrollView, contains multiple - axisContainerScrollView, contains multiple - itemField UITableView - used to pass text into itemField 考虑到这一点

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

这个观点相当复杂,由几个观点组成,我将对此进行解释

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];
}
最后这些是我的代表

- (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.
}

您应该创建一个自定义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]; 

我尝试了第二种解决方案,但不幸的是,它没有奏效。我想把它保留为我创建的滚动视图,所以我仍在尝试以这种方式整理它。
itemField = [view viewWithTag:1];
[itemField becomeFirstResponder];