How to set the Cell height as dynamically in iPhon

2019-05-30 08:31发布

问题:

In my apps, i have created table view and displayed the images and the labels in the table view. I have downloaded the image and set the image height into the another class and i have passed the image height value using delegates. In my problem is, heightForRowAtIndexPath method is call very earlier and it never calls again. So how i set the cell height depends on the image height?

//appDelegate.imageHeight - Image height delegate value.

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

 NSLog(the height of the image is --------------------------%f, appDelegate.imageHeight);
 In Console:
     the height of the image is --------------------------0.000000
     the height of the image is --------------------------0.000000
     the height of the image is --------------------------0.000000
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     NSLog(the height of the image is --------------------------%f, appDelegate.imageHeight);
     In Console:

        the height of the image is --------------------------98.567039
        the height of the image is --------------------------300.000000
        the height of the image is --------------------------232.288406
 }

Is any possible to call the heightforRowAtIndexPath method inside the cellForRowAt method. Because i have set the cell frame size and height depends on the image height. So how can i set the cell height depends on the image height?, please guide me.

Thanks!

回答1:

heightForRowAtIndexPath method is called before cellForRowAtIndexPath, so you need to know height of images you are displaying in your cells. Maintain the dictionary or array that contains height of images which get passed to this class and use it to set the height of cell accordingly



回答2:

here are some suggestions UITableView flexible/dynamic heightForRowAtIndexPath