Retrieve custom prototype cell height from storyboard?

后端 未结 2 497
小蘑菇
小蘑菇 2020-11-27 05:51

When using \"Dynamic Prototypes\" for specifying UITableView content on the storyboard, there is a \"Row Height\" property that can be set to Custom.

Wh

2条回答
  •  情深已故
    2020-11-27 06:27

    I created a category for UITableView some time ago that may come helpful for this. It stores 'prototype' cells using asociated objects for reusing the prototypes and provides a convenience method for obtaining the height of the row assigned in storyboard. The prototypes are released when the table view is deallocated.

    UITableView+PrototypeCells.h

    #import 
    
    @interface UITableView (PrototypeCells)
    
    - (CGFloat)heightForRowWithReuseIdentifier:(NSString*)reuseIdentifier;
    - (UITableViewCell*)prototypeCellWithReuseIdentifier:(NSString*)reuseIdentifier;
    
    @end
    

    UITableView+PrototypeCells.m

    #import "UITableView+PrototypeCells.h"
    #import 
    
    static char const * const key = "prototypeCells";
    
    @implementation UITableView (PrototypeCells)
    - (void)setPrototypeCells:(NSMutableDictionary *)prototypeCells {
        objc_setAssociatedObject(self, key, prototypeCells, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (NSMutableDictionary *)prototypeCells {
        return objc_getAssociatedObject(self, key);
    }
    
    - (CGFloat)heightForRowWithReuseIdentifier:(NSString*)reuseIdentifier {
        return [self prototypeCellWithReuseIdentifier:reuseIdentifier].frame.size.height;
    }
    
    - (UITableViewCell*)prototypeCellWithReuseIdentifier:(NSString*)reuseIdentifier {
        if (self.prototypeCells == nil) {
            self.prototypeCells = [[NSMutableDictionary alloc] init];
        }
    
        UITableViewCell* cell = self.prototypeCells[reuseIdentifier];
        if (cell == nil) {
            cell = [self dequeueReusableCellWithIdentifier:reuseIdentifier];
            self.prototypeCells[reuseIdentifier] = cell;
        }
        return cell;
    }
    
    @end
    

    Usage

    Obtaining the static height set in storyboard is as simple as this:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return [tableView heightForRowWithReuseIdentifier:@"cellIdentifier"];
    }
    

    Assuming a multi-section table view:

    enum {
        kFirstSection = 0,
        kSecondSection
    };
    
    static NSString* const kFirstSectionRowId = @"section1Id";
    static NSString* const kSecondSectionRowId = @"section2Id";
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        CGFloat height = tableView.rowHeight; // Default UITableView row height
        switch (indexPath.section) {
            case kFirstSection:
                height = [tableView heightForRowWithReuseIdentifier:kFirstSectionRowId];
                break;
            case kSecondSection:
                height = [tableView heightForRowWithReuseIdentifier:kSecondSectionRowId];
        }
        return height;
    }
    

    And finally if the row height is dynamic:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        id thisRowData = self.allData[indexPath.row]; // Obtain the data for this row
    
        // Obtain the prototype cell
        MyTableViewCell* cell = (MyTableViewCell*)[self prototypeCellWithReuseIdentifier:@"cellIdentifier"];
    
        // Ask the prototype cell for its own height when showing the specified data
        return [cell heightForData:thisRowData];
    }
    

提交回复
热议问题