UITableViewCell: Dynamic Cell Height by 'Cell Identifier'

Tom Testicool

(note: The tableView I am using is from the Parse.com iOS SDK - PFQueryTableViewController)

Scenario = I have a TableViewController that has two different types of cells (each with their own identifier). Each object upon being queried and loaded into the datasource is checked if a key on the object is true. Depending on the result I dequeueReusableCellWithIdentifier to the correct cell.

-(PFTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

    myTableViewCell *cell;

    if ([object[@"orientation"] isEqualToString:@"left"] || [object[@"orientation"] isEqualToString:@"right"]) {

        myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    else {

        myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell2"];
    }

This all does its job. Each cell is being loaded at the correct indexPath.row and everything. Problem is my tableView "Row Height" itself does not readjust for the new cell. This causes overlapping of cells and makes everything ugly. I can tell the tableView in storyboard to set the row height to whatever the larger of the two cell heights is, but that leaves big spaces in-between cells too which also makes it look ugly.

Question = It is my belief (and correct me if I'm wrong) that I need to use the

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

method in order to achieve this. Where I need help is I am not sure how to set the height of each cell at indexPath depending upon the 'identifier' that I gave each cell in the cellForRowAtIndexPath method.

What I'm looking for = Something like this (please excuse the SuedoCode)

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

    if ([cell.identifier isEqual:@"Cell"] {

    return 100;

    }

    else {

    return 200;

    }
}

ANSWER: I figured it out! (I marked the answer below as accepted because it pointed me in the right direction)

Because I am using a PFQueryTableViewController all I had to do this...

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

    PFObject *object = [self.objects objectAtIndex:indexPath.row];

    if ([object[@"orientation"] isEqual:@"left"] || [object[@"orientation"] isEqual:@"right"]) {

        return 100;
    }

    else {

       return 200;
    }

}
timothykc

First, some things to keep in mind. heightForRowAtindexPath is calledbefore CellForRowatIndexPath, and simply says, if object is at indexPath X, then return Y or Z.

The more correct approach might be to subclass the tableCell class, set a property in the .h file and then figure out the path... I'll give you a dirty way :)

Create an NSMutableArray property (don't forget to init it somewhere/somehow), and based on your dataSource, populate it with Height A or Height B (a float). Now, back in heightForRowAtIndexPath, you can say something to the effect of:

return (int)self.myMutableArray[indexPath.row];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

UITableView inside UITableViewCell with dynamic cell height

From Dev

UICollectionView with dynamic Cell Identifier

From Dev

UICollectionView - dynamic cell height?

From Dev

Uitableview cell dynamic height

From Dev

Dynamic cell's height

From Dev

Dynamic cell height for tableview

From Dev

Dynamic UITableCellView height with cell indicator

From Dev

CollectionView Dynamic cell height swift

From Dev

confusing about the height of cell, UITableViewCell,iOS

From Dev

Dynamic UITableViewCell content does not expand cell

From Dev

Is there a way to update the height of a single UITableViewCell, without recalculating the height for every cell?

From Dev

Dynamic-height cell in collection view

From Dev

Dynamic prototype cell height size of UITableViewController on storyboard

From Dev

HTML table dynamic height for one cell

From Dev

Dynamic-height cell in collection view

From Dev

set Dynamic width and height of collection view cell

From Dev

Swift, custom UITableViewCell with UIImageView. Need to resize cell to image height

From Dev

UITableViewCell height is not fitted when hiding UIView inside the cell using AutoLayout

From Dev

Calculating height of the UITableViewCell that is dependent on the cell width and image loaded asynchronously

From Dev

Set height of UITableView which contain dynamic Cell Height

From Dev

Dynamic UITableView cell height with AutoLayout with Dynamic Type Labels

From Dev

How to create dynamic tableview cell with dynamic tableview height in iOS

From Dev

UItableviewCell cell reuse issue

From Dev

iOS: Dynamic Height For UITableViewCell

From Dev

UITableViewCell dynamic height programmatically

From Dev

UITableViewCell With UIWebView Dynamic Height

From Dev

UILabel with dynamic height into UITableviewcell

From Dev

UICollectionView with dynamic height not getting same space between cell

From Dev

Label in dynamic cell height on table view not showing text properly

Related Related

  1. 1

    UITableView inside UITableViewCell with dynamic cell height

  2. 2

    UICollectionView with dynamic Cell Identifier

  3. 3

    UICollectionView - dynamic cell height?

  4. 4

    Uitableview cell dynamic height

  5. 5

    Dynamic cell's height

  6. 6

    Dynamic cell height for tableview

  7. 7

    Dynamic UITableCellView height with cell indicator

  8. 8

    CollectionView Dynamic cell height swift

  9. 9

    confusing about the height of cell, UITableViewCell,iOS

  10. 10

    Dynamic UITableViewCell content does not expand cell

  11. 11

    Is there a way to update the height of a single UITableViewCell, without recalculating the height for every cell?

  12. 12

    Dynamic-height cell in collection view

  13. 13

    Dynamic prototype cell height size of UITableViewController on storyboard

  14. 14

    HTML table dynamic height for one cell

  15. 15

    Dynamic-height cell in collection view

  16. 16

    set Dynamic width and height of collection view cell

  17. 17

    Swift, custom UITableViewCell with UIImageView. Need to resize cell to image height

  18. 18

    UITableViewCell height is not fitted when hiding UIView inside the cell using AutoLayout

  19. 19

    Calculating height of the UITableViewCell that is dependent on the cell width and image loaded asynchronously

  20. 20

    Set height of UITableView which contain dynamic Cell Height

  21. 21

    Dynamic UITableView cell height with AutoLayout with Dynamic Type Labels

  22. 22

    How to create dynamic tableview cell with dynamic tableview height in iOS

  23. 23

    UItableviewCell cell reuse issue

  24. 24

    iOS: Dynamic Height For UITableViewCell

  25. 25

    UITableViewCell dynamic height programmatically

  26. 26

    UITableViewCell With UIWebView Dynamic Height

  27. 27

    UILabel with dynamic height into UITableviewcell

  28. 28

    UICollectionView with dynamic height not getting same space between cell

  29. 29

    Label in dynamic cell height on table view not showing text properly

HotTag

Archive