Custom Cell Row Height setting in storyboard is not responding

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

    You can get height of UITableviewCell (in UITableviewController - static cells) from storyboard with help of following lines.

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       CGFloat height = [super tableView:tableView heightForRowAtIndexPath:indexPath];
    
        return height;
    }
    
    0 讨论(0)
  • 2020-11-30 17:32

    The same problem occurred when working on XCode 9 using Swift 4.

    Add AutoLayout for the UI elements inside the Cell and custom cell row height will work accordingly as specified.

    0 讨论(0)
  • 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...>
    
    0 讨论(0)
  • The only real solution I could find is this

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = ...; // Instantiate with a "common" method you'll use again in cellForRowAtIndexPath:
        return cell.frame.size.height;
    }
    

    This works and allows not to have an horrible switch/if duplicating the logic already in the StoryBoard. Not sure about performance but I guess when arriving in cellForRow: the cell being already initalized it's as fast. Of course there are probably collateral damages here, but it looks like it works fine for me here.

    I also posted this here: https://devforums.apple.com/message/772464

    EDIT: Ortwin Gentz reminded me that heightForRowAtIndexPath: will be called for all cells of the TableView, not only the visible ones. Sounds logical since iOS needs to know the total height to be able to show the right scrollbars. It means it's probably fine on small TableViews (like 20 Cells) but forget about it on a 1000 Cell TableView.

    Also, the previous trick with XML: Same as first comment for me. The correct value was already there.

    0 讨论(0)
  • 2020-11-30 17:36

    Added as a comment, but posting as an answer for visibility:

    There seems to also be an issue if you initially setup a table view as "static cells" and then change it to "dynamic prototypes." I had an issue where even the delegate method for heightForRowAtIndexPath was being ignored. I first resolved it by directly editing the storyboard XML, then finally just rebuilt the storyboard scene from scratch, using dynamic prototypes from the start.

    0 讨论(0)
  • 2020-11-30 17:38

    If you're using swift , use like this. Don't Use storyboard to select row height. Programmatically set table row height like this,

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if indexPath.row == 0 || indexPath.row == 1{
            let cell = self.tableView.dequeueReusableCellWithIdentifier("section1", forIndexPath: indexPath) as! Section1TableViewCell
            self.tableView.rowHeight = 150
            cell.label1.text = "hiiiiii"
            cell.label2.text = "Huiiilllllll"
            return cell
    
        } else {
    
            let cell = self.tableView.dequeueReusableCellWithIdentifier("section2", forIndexPath: indexPath) as! Section2TableViewCell
            self.tableView.rowHeight = 60
            cell.label3.text = "llll"
            return cell
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题