Custom UITableView Dynamic Cell Height

前端 未结 6 637
一个人的身影
一个人的身影 2021-02-06 09:22

I have search and searched through endless blogs and articles on how to determine a dynamic height for a custom UITableViewCell and its detailed text. I have really had a hard t

相关标签:
6条回答
  • 2021-02-06 09:41

    iOS 7

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
    {
        NSString *text = @"Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello HelloHello HelloHello HelloHello HelloHello HelloHello Hello";
    
        CGRect rect = [text boundingRectWithSize:CGSizeMake(280.0f, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:@{
            NSFontAttributeName: [UIFont fontWithName:@"Hellvetica" size:14.0f]
        } context:nil];
    
        return CGSizeMake(320.0f, ceil(rect.size.height) + 10.0f);
    }
    
    0 讨论(0)
  • 2021-02-06 09:54

    I tried many solutions, but the one that worked was this, suggested by a friend:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        int height = [StringUtils findHeightForText:yourLabel havingWidth:yourWidth andFont:[UIFont systemFontOfSize:17.0f]];
    
        height += [StringUtils findHeightForText:yourOtherLabel havingWidth:yourWidth andFont:[UIFont systemFontOfSize:14.0f]];
    
        return height + CELL_SIZE_WITHOUT_LABELS; //important to know the size of your custom cell without the height of the variable labels
    }
    

    The StringUtils.h class:

     #import <Foundation/Foundation.h>
    
        @interface StringUtils : NSObject
    
        + (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font;
    
        @end
    

    StringUtils.m class:

        #import "StringUtils.h"
    
        @implementation StringUtils
    
        + (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
            CGFloat result = font.pointSize+4;
            if (text) {
                CGSize size;
    
                CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX)
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:font}
                                                  context:nil];
                size = CGSizeMake(frame.size.width, frame.size.height+1);
                result = MAX(size.height, result); //At least one row
            }
            return result;
        }
    
        @end
    

    It worked perfectly for me. I had a Custom Cell with 3 images with fixed sizes, 2 labels with fixed sizes and 2 variable labels. Worked like a charm. Hope it works for you too.

    Best regards, Alexandre.

    0 讨论(0)
  • 2021-02-06 09:57

    I just wrote about this problem and how I finally decided to solve it. You can read about it here: Dynamic UITableView Cell Height Based on Contents

    Basically, I created a UITableView subclass that automates the handling and calculation of dynamic cell height for both default and custom cells. It is not a silver bullet and probably needs to be extended and tweaked, but I have used it as is in several apps with good result.

    You can grab the code here: https://github.com/danielsaidi/AutoSizeTableView

    Hope it helps!

    (...and if it didn't, I'd love to hear why not)

    0 讨论(0)
  • 2021-02-06 10:00

    I had a similar issue a while back and this helped me tremendously.

    #define PADDING 10.0f
    - (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSString *text = [self.items objectAtIndex:indexPath.row];
        CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];
    
        return textSize.height + PADDING * 3;
    }
    
    0 讨论(0)
  • 2021-02-06 10:04

    Hey there so you are going to need to store the list of strings in an NSArray and then you are going to need to calculate the height of the nsstring using sizeWithFont:constrainedToSize: the documentation is found here http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html

    so your tableView method should look something like

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont fontWithName:"Helvetica" size:9] forKey: NSFontAttributeName];
    
         CGSize cell_size = [string boundingRectWithSize:CGSizeMake(300,999) 
                                options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin     
                                attributes:stringAttributes context:nil].size;
    if (cell_size.height > 70)
    {
    return cell_size.height;
    }
    else 
    {
    return 70;
    }
    }
    

    EDIT : THIS has been updated for iOS 7

    0 讨论(0)
  • 2021-02-06 10:04

    Use UITableViewDelegate method:

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     // here you need to get height of your textlabel in your custom cell. 
     MyCustomCell *myCell = (MyCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
     return myCell.myTextLabel.frame.size.height + 20;
    }
    

    Something like this

    0 讨论(0)
提交回复
热议问题