Set height of UITableView which contain dynamic Cell Height

sohan vanani

I have taken UITableView into UIScrollView now I want to set static height to tabelview and set contentSize to ScrollView because UITableView calling cellForRowAtIndexPath every time when I scroll tableview.

I am setting tableviewcell size dynamically using below code.

Now question is: How and where i can set total height to tableview and content size to my scrollview?

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *itemDataDic = [resultArray objectAtIndex:indexPath.row];
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:15.0];
    CGSize constraintSize = CGSizeMake(275.0f, MAXFLOAT);
    CGSize labelSize = [[itemDataDic objectForKey:@"offer_title"] sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];

    if (labelSize.height > 30.00f)
    {
        totalHeight = totalHeight + 325;
        return 325;
    }
    else
    {
        totalHeight = totalHeight + 306;
        return 306;
    }
}
Tuncer Tırnavalı

'heightForRowAtIndexPath' is called many times for a cell, therefore, totalHeight is calculated wrong in this way.

You need to calculate the height of your table on init and when the data has been changed.

-(void) reloadAndResizeTable
{
    CGFloat totalHeight = .0f;
    for (NSMutableDictionary* itemDataDic in resultArray) {
        UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:15.0];
        CGSize constraintSize = CGSizeMake(275.0f, MAXFLOAT);
        CGSize labelSize = [[itemDataDic objectForKey:@"offer_title"] sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];

        if (labelSize.height > 30.00f)
        {
            totalHeight = totalHeight + 325.0f;
            return 325;
        }
        else
        {
            totalHeight = totalHeight + 306.0f;
            return 306;
        }
    }
    CGRect frame = [yourTableView frame];
    [yourTableView setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, totalHeight)];
    [yourTableView reloadData];
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

iOS 7/8 UITableView Cell: Two UILabels with dynamic height with auto layout for variable row height

From Dev

CollectionView Dynamic cell height swift

From Dev

UICollectionView - dynamic cell height?

From Dev

Dynamic UITableView cell height with AutoLayout with Dynamic Type Labels

From Dev

Set row height of a UITableView according to the cell in that row

From Dev

Dynamic height of table is not set

From Dev

UITableViewCell: Dynamic Cell Height by 'Cell Identifier'

From Dev

UITableView dynamic cell heights - reload height for single row

From Dev

UICollectionview set cell height based on image height

From Dev

Dynamic UITableCellView height with cell indicator

From Dev

Get UITableView cell height

From Dev

How to make fixed height cell on uitableview?

From Dev

jQuery set height of a div to the 'dynamic height' of another

From Dev

Static UITableView, make a single cell with dynamic height in Swift

From Dev

Dynamic UITableView height

From Dev

UITableView with variable cell height: Working in IB but not programmatically

From Dev

Uitableview cell dynamic height

From Dev

UITableView inside UITableViewCell with dynamic cell height

From Dev

UITableView when changing constraint which effects height of cell after dequeueing, end up with broken constraints

From Dev

Dynamic height of UITableview Issue

From Dev

How to set height constraint of UITableView according to dynamic height cells?

From Dev

Swift: How to set dynamic cell height in TableViewController which contains more than one cell

From Dev

How to dynamically set the height of a UITableView?

From Dev

Dynamic cell's height

From Dev

Dynamic cell height for tableview

From Dev

set Dynamic width and height of collection view cell

From Dev

How to set height of UITableView programatically

From Dev

Set height of a specific cell in my UITableView

From Dev

Dynamic row height in UITableView using custom cell from xib

Related Related

  1. 1

    iOS 7/8 UITableView Cell: Two UILabels with dynamic height with auto layout for variable row height

  2. 2

    CollectionView Dynamic cell height swift

  3. 3

    UICollectionView - dynamic cell height?

  4. 4

    Dynamic UITableView cell height with AutoLayout with Dynamic Type Labels

  5. 5

    Set row height of a UITableView according to the cell in that row

  6. 6

    Dynamic height of table is not set

  7. 7

    UITableViewCell: Dynamic Cell Height by 'Cell Identifier'

  8. 8

    UITableView dynamic cell heights - reload height for single row

  9. 9

    UICollectionview set cell height based on image height

  10. 10

    Dynamic UITableCellView height with cell indicator

  11. 11

    Get UITableView cell height

  12. 12

    How to make fixed height cell on uitableview?

  13. 13

    jQuery set height of a div to the 'dynamic height' of another

  14. 14

    Static UITableView, make a single cell with dynamic height in Swift

  15. 15

    Dynamic UITableView height

  16. 16

    UITableView with variable cell height: Working in IB but not programmatically

  17. 17

    Uitableview cell dynamic height

  18. 18

    UITableView inside UITableViewCell with dynamic cell height

  19. 19

    UITableView when changing constraint which effects height of cell after dequeueing, end up with broken constraints

  20. 20

    Dynamic height of UITableview Issue

  21. 21

    How to set height constraint of UITableView according to dynamic height cells?

  22. 22

    Swift: How to set dynamic cell height in TableViewController which contains more than one cell

  23. 23

    How to dynamically set the height of a UITableView?

  24. 24

    Dynamic cell's height

  25. 25

    Dynamic cell height for tableview

  26. 26

    set Dynamic width and height of collection view cell

  27. 27

    How to set height of UITableView programatically

  28. 28

    Set height of a specific cell in my UITableView

  29. 29

    Dynamic row height in UITableView using custom cell from xib

HotTag

Archive