Dynamic UITableView height

后端 未结 11 1617
無奈伤痛
無奈伤痛 2020-12-29 09:47

I would like to set the UITableView to match the height for all the contents in the table view.

This is my storyboard

The problem with this is the t

11条回答
  •  鱼传尺愫
    2020-12-29 10:12

    You probably have to implement the table view intrinsic content size. Please check this answer to see if it helps.

    I remember having this problem and even created a custom UITableView subclass.

    #import "IntrinsicTableView.h"
    
    @implementation IntrinsicTableView
    
    #pragma mark - UIView
    
    - (CGSize)intrinsicContentSize
    {
        return CGSizeMake(UIViewNoIntrinsicMetric, self.contentSize.height);
    }
    
    #pragma mark - UITableView
    
    - (void)endUpdates
    {
        [super endUpdates];
        [self invalidateIntrinsicContentSize];
    }
    
    - (void)reloadData
    {
        [super reloadData];
        [self invalidateIntrinsicContentSize];
    }
    
    - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    {
        [super reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
        [self invalidateIntrinsicContentSize];
    }
    
    - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
    {
        [super reloadSections:sections withRowAnimation:animation];
        [self invalidateIntrinsicContentSize];
    }
    
    - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    {
        [super insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
        [self invalidateIntrinsicContentSize];
    }
    
    - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
    {
        [super insertSections:sections withRowAnimation:animation];
        [self invalidateIntrinsicContentSize];
    }
    
    - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
    {
        [super deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
        [self invalidateIntrinsicContentSize];
    }
    
    - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
    {
        [super deleteSections:sections withRowAnimation:animation];
        [self invalidateIntrinsicContentSize];
    }
    
    @end
    

提交回复
热议问题