如何在iphone中使两行的tableView高度更改

时间:2013-10-30 10:55:24

标签: ios iphone tableview

我有tableView,其中我想要显示第一个单元格,它是textfield作为单元格的子视图。当我在其中输入任何数据时,它工作正常。它显示在新行中输入的数据意味着第二行和第一行是相同的字段但是,当我输入另一行时,tableVIew高度变得混乱。

我希望当我添加第二行时,它应该显示滚动而不是增加高度。

这是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;
    }

1 个答案:

答案 0 :(得分:1)

设置具有特定行数的indexPath.row以更改特定单元格的高度,例如..

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{

       if (indexPath.row == 0) /// here you need to set row number
       {
         return 90;
       }
       else if (indexPath.row == 2) 
       {
         return 110;
       }

          return 45;
}