How do I read a UITableViewCell (subclass)'s height from a NIB?

后端 未结 2 1377
悲&欢浪女
悲&欢浪女 2021-01-18 06:02

I have a UITableViewCell (really a subclass) defined in a NIB. In the NIB I\'ve set the frame height to 183, I also set the \"Row Height\" to 183 and ticked off custom.

相关标签:
2条回答
  • 2021-01-18 06:38

    First UITableViewDelegate method, which gets called is heightForRowAtIndexPath not the UITableViewDataSource method, cellForRowAtIndexPath.

    You can get your custom cell height only in cellForRowAtIndexPath, where you alloc-init that cell. So, it won't consider custom cell height over here.

    It will take the height for the cell which is mentioned in heightForRowAtIndexPath method only. As it gets called first.

    0 讨论(0)
  • 2021-01-18 06:55

    You can use this example code to get cell height from NIB

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        return [[[[NSBundle mainBundle] loadNibNamed:@"ELConvCurListViewCell" owner:self options:nil] objectAtIndex:0] bounds].size.height;
    }
    

    Update: You can get and assign height of NIB once time in the viewDidLoad to the cellHeight variable and in the heightForRowAtIndexPath use return cellHeight

    0 讨论(0)
提交回复
热议问题