How to change cell height dynamically in UITableView static cell

前端 未结 4 614
盖世英雄少女心
盖世英雄少女心 2021-01-12 07:42

I am using UITableViewController instead detailView to show one entity details. I populated one row of data from PFQuery in my viewDidLoad method.

4条回答
  •  臣服心动
    2021-01-12 08:21

    I have been doing trying to achieve this from many days but could not. I tried Fahim answer and got clue, following I got from other different answer and have been able to complete it. Code in I added following function to get exact size of my label

    -(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
    {
    
        CGSize maximumSize = CGSizeMake(labelWidth, 10000);
        NSLog(@"crossing getLabelheightForText");
    
        //provide appropriate font and font size
        CGSize labelHeighSize = [text sizeWithFont: [UIFont fontWithName:@"Trebuchet MS" size:17.0f]
                                 constrainedToSize:maximumSize
                                     lineBreakMode:NSLineBreakByTruncatingTail];
        return labelHeighSize.height;
    }
    

    Than I have added indexPath with all cell sizes to be returned showing here only one cell size.

        -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        switch (indexPath.section)
        {
            case 0:
            {
                if (indexPath.row == 0)  // category name size
                    return 44.0;   
                if (indexPath.row == 1)  // image size
                    return 204;
            }
            default:
                return 44.0       
        }
    }
    

    But after this I was getting perfect size of label but still have more to do as I was using PFQuery and tableView indexPath runs first that is why it could not update my table and I was still not able to resize cell. Than here is magic line that reloads my tableView and resolve my issue

    [self.tableView reloadData];
    

    Although issue is simple but it has a lot things so I put as reply to help other peoples.

提交回复
热议问题