Dynamic height of table is not set

前端 未结 3 1470
迷失自我
迷失自我 2021-01-21 22:28

I am trying to set dynamic height of table. But it is not working when I log height of table it is showing me dynamic height but not set to actual table.

Here is my code

相关标签:
3条回答
  • 2021-01-21 23:03

    To set the height for row you need to set

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return rowHt[indexPath.row];
    }
    

    The each row can have different height only if you set it at

    heightForRowAtIndexPath

    for that you need to select the corresponding rows from

    indexPath.row

    If you are setting your tabelView from code it would look something like this

    @property(nonatomic, strong)    UITableView     *table_sender;
    
    self.table_sender = [[UITableView alloc]  initWithFrame:frameTable style:UITableViewStylePlain] ;
        self.table_sender.dataSource = self;
        self.table_sender.delegate = self;
    

    where rowHt is a array of collection of height for each rows

    0 讨论(0)
  • 2021-01-21 23:13

    if you are using AutoLayout then you can't set frames - if you do that you get wiered behaviour

    Autolayout is sort of relative approach of placing UI components on screen. For e.g Stand 100px behind me and 20px from left boundary (here i have used pixel - you can think of as meters think of it as playground)

    0 讨论(0)
  • 2021-01-21 23:17

    I think there must be some other code change your tableView's frame.

    Use KVO to detect change of the frame of UITableView.

    1、add an observer to it in you viewDidLoad method

    [table_sender addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil] ;
    

    2、implement observeValueForKeyPath method and add an breakpoint in it. When the breakpoint stop, check the call stack to find who change the frame.

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        if ([keyPath isEqualToString:@"frame"]) {
            NSLog(@"%@", change) ; // add an breakpoint here
        } else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context] ;
        }
    }
    
    0 讨论(0)
提交回复
热议问题