-2

セルのサブビューとしてテキストフィールドである最初のセルのみを表示したいtableViewがあります。そこにデータを入力すると正常に動作します。入力されたデータは改行で表示され、2行目と1行目は同じフィールドですが、別の行を入力するとtableViewの高さが乱れます。

2行目を追加すると、高さを増やす代わりにスクロールを表示する必要があります。

tableView コードは次のとおりです。

-(UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if(!cell) 
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        // cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"tabelsales.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        //cell.backgroundColor = [UIColor clearColor];
    }

    for (UIView *subView in cell.subviews)
    {
        if (subView.tag == 2 || subView.tag == 22) 
        {
             [subView removeFromSuperview];
        }
    }

    tableView.backgroundView=nil;


    if(indexPath.section==0)
    {
        tagInputField =[[UITextField alloc]initWithFrame:CGRectMake(0,0,248,31)];

        tagInputField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
        tagInputField.textAlignment=UITextAlignmentLeft;

        //tagInputField.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"inputfeild2.png"]];

         tagInputField.backgroundColor=[UIColor whiteColor];

        tagInputField.tag = 2;
        tagInputField.delegate = self;
        tagInputField.clearButtonMode = UITextFieldViewModeWhileEditing;

        // Jani Comment New tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

        // jani [tagInputField setBorderStyle:UITextBorderStyleRoundedRect];

         [tagInputField.layer setCornerRadius:5.0f];
         [tagInputField.layer setMasksToBounds:YES];
         tagInputField.layer.borderWidth = 0.5;
         tagInputField.layer.borderColor = [UIColor darkGrayColor].CGColor;

         tagInputField.font=[UIFont fontWithName:@"Myriad-Pro" size:8];
         [tagInputField setText:@"Enter tag here "];
         tagInputField.textColor =[UIColor grayColor];

         [cell addSubview:tagInputField];

         return cell;
    }

  if(indexPath.section==1)
  {
    UIButton *crossButton =[[UIButton alloc]initWithFrame:CGRectMake(228, 8, 18, 18)];
    crossButton.tag = 22; //use a tag value that is not used for any other subview
    //crossButton.backgroundColor = [UIColor purpleColor];
    crossButton.backgroundColor  = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Cross.png"]];
    [cell addSubview:crossButton];
    cell.textLabel.font =[UIFont fontWithName:@"Myriad-Pro" size:8];
    cell.textLabel.textColor =[UIColor grayColor];

    cell.backgroundColor=[UIColor whiteColor];

    cell.textLabel.text =[tagArray objectAtIndex:indexPath.row];
    [crossButton addTarget:self action:@selector(deleteCell:) forControlEvents:UIControlEventTouchUpInside];


    [tagInputField setFrame:CGRectMake(0,0,248,30)];

    // jani [tagInputField setFrame:CGRectMake(8,2,248,30)];

    tableView.backgroundColor=[UIColor whiteColor];

   // tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    [publishButton setFrame:CGRectMake(40,560,250, 50)];

    [descriptionTextImageView setFrame:CGRectMake(48,450,250,90)];

   [tagInputField.layer setCornerRadius:0.0f];
    [tagInputField.layer setMasksToBounds:YES];
    tagInputField.layer.borderWidth = 0.0;
    tagInputField.layer.borderColor = [UIColor clearColor].CGColor;

    return cell;
}

高さが変わるときのコードは次のとおりです

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

   if(textField.tag == 2)
    {

    if (textField.text.length > 0 || ![tagTextField.text isEqualToString:@""]) {
    [textField resignFirstResponder];
    [tagArray addObject:tagInputField.text];
    [tableView reloadData]; 

    CGRect frame = tableView.frame;
    frame.size.height = MIN(64 * [tagArray count], 400); // 400 is the maximum heighthat the table view can have. You can change it to whatever you like
    tableView.frame = frame;  
    [tableView.layer setCornerRadius:7.0f];
    [tableView.layer setMasksToBounds:YES];
    tableView.layer.borderWidth = 0.5;
    tableView.layer.borderColor = [UIColor grayColor].CGColor;

   [self showAnimationBack];
    }

    else {
   // textField.text = @"Enter tag";

   [textField resignFirstResponder];
    [self showAnimationBack];
    }
    }

   if (textField == titleTextField) {
    [titleTextField resignFirstResponder];
    }

  if (textField == descriptionTextView) {
    [descriptionTextView resignFirstResponder];
    [self showAnimationBack];

    }
    return YES;
    }
4

1 に答える 1