Custom Cell Row Height setting in storyboard is not responding

后端 未结 18 2131
名媛妹妹
名媛妹妹 2020-11-30 16:54

I am trying to adjust the cell height for one of the cells on my table view. I am adjusting the size from the \"row height\" setting inside the \"size inspector\" of the cel

18条回答
  •  不知归路
    2020-11-30 17:21

    I have recently been wrestling with this. My issue was the solutions posted above using the heightForRowAtIndexPath: method would work for iOS 7.1 in the Simulator but then have completely screwed up results by simply switching to iOS 8.1.

    I began reading more about self-sizing cells (introduced in iOS 8, read here). It was apparent that the use of UITableViewAutomaticDimension would help in iOS 8. I tried using that technique and deleted the use of heightForRowAtIndexPath: and voila, it was working perfect in iOS 8 now. But then iOS 7 wasn't. What was I to do? I needed heightForRowAtIndexPath: for iOS 7 and not for iOS 8.

    Here is my solution (trimmed up for brevity's sake) which borrow's from the answer @JosephH posted above:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.tableView.estimatedRowHeight = 50.;
        self.tableView.rowHeight = UITableViewAutomaticDimension;
    
        // ...
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
            return UITableViewAutomaticDimension;
    
        } else {
            NSString *cellIdentifier = [self reuseIdentifierForCellAtIndexPath:indexPath];
            static NSMutableDictionary *heightCache;
            if (!heightCache)
                heightCache = [[NSMutableDictionary alloc] init];
            NSNumber *cachedHeight = heightCache[cellIdentifier];
            if (cachedHeight)
                return cachedHeight.floatValue;
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        CGFloat height = cell.bounds.size.height;
        heightCache[cellIdentifier] = @(height);
        return height;
        }
    }
    
    - (NSString *)reuseIdentifierForCellAtIndexPath:(NSIndexPath *)indexPath {
        NSString * reuseIdentifier;
        switch (indexPath.row) {
            case 0:
                reuseIdentifier = EventTitleCellIdentifier;
                break;
            case 2:
                reuseIdentifier = EventDateTimeCellIdentifier;
                break;
            case 4:
                reuseIdentifier = EventContactsCellIdentifier;
                break;
            case 6:
                reuseIdentifier = EventLocationCellIdentifier;
                break;
            case 8:
                reuseIdentifier = NotesCellIdentifier;
                break;
            default:
                reuseIdentifier = SeparatorCellIdentifier;
                break;
        }
    
        return reuseIdentifier;
    }
    

    SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0") is actually from a set of macro definitions I am using which I found somewhere (very helpful). They are defined as:

    #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
    #define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
    #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
    #define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
    #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
    

提交回复
热议问题