Custom Cell Row Height setting in storyboard is not responding

后端 未结 18 2075
名媛妹妹
名媛妹妹 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:33

    I've built the code the various answers/comments hint at so that this works for storyboards that use prototype cells.

    This code:

    • Does not require the cell height to be set anywhere other than the obvious place in the storyboard
    • Caches the height for performance reasons
    • Uses a common function to get the cell identifier for an index path to avoid duplicated logic

    Thanks to Answerbot, Brennan and lensovet.

    - (NSString *)cellIdentifierForIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellIdentifier = nil;
    
        switch (indexPath.section)
        {
            case 0:
                cellIdentifier = @"ArtworkCell";
                break;
             <... and so on ...>
        }
    
        return cellIdentifier;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellIdentifier = [self cellIdentifierForIndexPath: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;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellIdentifier = [self cellIdentifierForIndexPath:indexPath];
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
        <... configure cell as usual...>
    

提交回复
热议问题