【问题标题】:Dynamic height of table is not set未设置表格的动态高度
【发布时间】:2014-01-10 08:43:32
【问题描述】:

我正在尝试设置表格的动态高度。但是当我记录表格高度时它不起作用,它向我显示动态高度但未设置为实际表格。

这是我的代码:

CGRect table_frame;
table_frame=table_sender.frame;
NSLog(@"table fram: %f",table_frame.size.height); //got table height 444
float height = [senderHistoryDataArray count]  * 40 +40 ; // 4*25.00
NSLog(@"height of table: %f",height);  //got this height 200
if(height>=444){
    table_frame.size.height=444;
}
else{
    table_frame.size.height=height;
    NSLog(@"height set");  //also displying this line in log
}

table_sender.frame=table_frame;
[table_sender reloadData];

输出:

当我取消选中自动布局时,它会起作用,但它只是显示比以前更多的部分。

【问题讨论】:

  • 我不知道你图片中的tableview框架。
  • 实际上我设置了高度 444,但是当我计算高度时它是 200。但我不知道那个白色背景是什么
  • 你想只改变高度表还是你在谈论自定义单元格(每个都有其他高度?)
  • 不只是说桌子的高度
  • 你在哪里调用你给我们看的代码?

标签: ios iphone uitableview ios7


【解决方案1】:

如果您使用的是 AutoLayout,那么您将无法设置框架 - 如果您这样做,您会得到奇怪的行为

自动布局是一种在屏幕上放置 UI 组件的相对方法。 例如,站在我身后 100 像素,距离左边界 20 像素(这里我使用了像素 - 您可以将其视为米,将其视为游乐场)

【讨论】:

    【解决方案2】:

    要设置行的高度,您需要设置

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

    每行只有设置为不同的高度

    heightForRowAtIndexPath

    为此,您需要从

    中选择相应的行

    indexPath.row

    如果您从代码中设置 tabelView,它看起来像这样

    @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;
    

    其中 rowHt 是每行高度集合的数组

    【讨论】:

      【解决方案3】:

      我认为必须有一些其他代码更改您的 tableView 的框架。

      使用KVO检测UITableView的帧变化。

      1、在你的viewDidLoad方法中添加一个观察者

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

      2、实现observeValueForKeyPath方法并在其中添加断点。当断点停止时,检查调用堆栈以找出谁改变了框架。

      - (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] ;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-05-24
        • 1970-01-01
        • 2020-03-24
        • 2018-06-15
        • 1970-01-01
        • 1970-01-01
        • 2012-08-01
        • 2015-08-01
        • 2017-06-15
        相关资源
        最近更新 更多