Dynamic cell's height

Maria

I read a lot of tutorials about it but still can't make it right. Here is the code:

@implementation DCHViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.tableViewContentArray = @[@"The franchise centers around a series of fantasy and science fantasy role-playing video games (RPGs), but includes motion pictures, anime, printed media, and other merchandise. The eponymous first game in the series, published in 1987",
                                   @"racing, third-person shooter, fighting, and rhythm.",
                                   @"e franchise. Recurring elements include plot themes, character names, and game mechanics. Plots center on a group of heroes battling a great evil while exploring the characters' internal struggles and relationships. Character names are frequently derived from the history, languages, and mythologies of cultures worldwide.",
                                   @"l Fantasy has been a driving force in the video game industry, and the series has affected Square Enix's business practices and its relationships with other video game developers. It has also introduced many features now common in role-playing video games and has been credited with helping to popularize console-based RPGs in markets outside Japan.",
                                   @" in Japan, has been bundled with Final Fantasy in several re-releases.[3][4][5] The last of the NES installments, Final Fantasy III, was release",
                                   @"he first six games to three-dimensional (3D) computer graphics; the game features polygonal characters on pre-rendered backgrounds. It also introduced a more modern setting, a style that was carried over to the next game.[3] It was also the first in the series to be released in Europe. The eighth installment was published in 1999, and was the first to consistently use realistically proportioned characters and feature a vocal piece as its theme music.[3][13] Final Fantasy IX, released in 2000, returned to the series' roots by revisiting a more traditional Final "];
}

#pragma mark - Table View methods

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
    }

    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font = [UIFont systemFontOfSize:14.0f];
    [cell.textLabel setLineBreakMode:NSLineBreakByCharWrapping];
    cell.textLabel.text = self.tableViewContentArray[indexPath.row];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *gettingSizeLabel = [[UILabel alloc] init];
    gettingSizeLabel.font = [UIFont systemFontOfSize:14.0f];
    gettingSizeLabel.numberOfLines = 0;
    gettingSizeLabel.text = self.tableViewContentArray[indexPath.row];
    [gettingSizeLabel setLineBreakMode:NSLineBreakByCharWrapping];
    CGSize maximumLabelSize = CGSizeMake(self.myTableView.frame.size.width - 20.0f, CGFLOAT_MAX);

    CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];


    CGFloat height = expectedSize.height;

    return height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableViewContentArray count];
}

@end

So as the result I have incorrect height, and cell's content (if it's too long) cropped. Why does it happen?

UPD: also I've tried to use boundingRectWithSize method of NSString, with exactly the same attributes:

NSDictionary *textAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],NSStrokeColorAttributeName: [UIColor grayColor]}. 

It does not work at all - it makes only 2 lines of text and very big width. Looks like it is just ignoring

CGSize templateSize = CGSizeMake(300.0f, CGFLOAT_MAX) that I have set.
Maria

Was fixed with this:

CGSize maximumLabelSize = CGSizeMake(290.0f, CGFLOAT_MAX);

CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];

CGFloat height = expectedSize.height + 1;

290 because of cell.textLabel's width. But! height+1 was some kind of improvisation. So I still does no know why should I make this "+1", but it works. Does anybody know what is this 1 means and why is not it included in sizeThatFits returned value?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

UICollectionView - dynamic cell height?

From Dev

Uitableview cell dynamic 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

UITableViewCell: Dynamic Cell Height by 'Cell Identifier'

From Dev

UITableView inside UITableViewCell with dynamic cell height

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

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

QPushButton 100% height of QGridLayout's cell

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

From Dev

UITableView dynamic cell heights - reload height for single row

From Dev

Custom Tableview cell with multiline UILabel needs dynamic height

From Dev

Scroll table view to bottom when using dynamic cell height

From Dev

ios swift - table view cell dynamic height depending on device

From Dev

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

From Dev

Swift: How to use "estimatedRowHeight" ?Dynamic Cell Height Stange errors

From Dev

How to work with dynamic cell height using auto layout in iOS

From Dev

Scroll table view to bottom when using dynamic cell height

From Dev

Dynamic row height in UITableView using custom cell from xib

From Dev

uicollectionview's dynamic height as per row values

From Dev

Viewpager with dynamic height not working (always use the first fragment's height)

Related Related

  1. 1

    UICollectionView - dynamic cell height?

  2. 2

    Uitableview cell dynamic height

  3. 3

    Dynamic cell height for tableview

  4. 4

    Dynamic UITableCellView height with cell indicator

  5. 5

    CollectionView Dynamic cell height swift

  6. 6

    UITableViewCell: Dynamic Cell Height by 'Cell Identifier'

  7. 7

    UITableView inside UITableViewCell with dynamic cell height

  8. 8

    Dynamic-height cell in collection view

  9. 9

    Dynamic prototype cell height size of UITableViewController on storyboard

  10. 10

    HTML table dynamic height for one cell

  11. 11

    Dynamic-height cell in collection view

  12. 12

    set Dynamic width and height of collection view cell

  13. 13

    Set height of UITableView which contain dynamic Cell Height

  14. 14

    Dynamic UITableView cell height with AutoLayout with Dynamic Type Labels

  15. 15

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

  16. 16

    QPushButton 100% height of QGridLayout's cell

  17. 17

    UICollectionView with dynamic height not getting same space between cell

  18. 18

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

  19. 19

    UITableView dynamic cell heights - reload height for single row

  20. 20

    Custom Tableview cell with multiline UILabel needs dynamic height

  21. 21

    Scroll table view to bottom when using dynamic cell height

  22. 22

    ios swift - table view cell dynamic height depending on device

  23. 23

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

  24. 24

    Swift: How to use "estimatedRowHeight" ?Dynamic Cell Height Stange errors

  25. 25

    How to work with dynamic cell height using auto layout in iOS

  26. 26

    Scroll table view to bottom when using dynamic cell height

  27. 27

    Dynamic row height in UITableView using custom cell from xib

  28. 28

    uicollectionview's dynamic height as per row values

  29. 29

    Viewpager with dynamic height not working (always use the first fragment's height)

HotTag

Archive