【问题标题】:UITableView, heighForRowAtIndex is not returning the height I wantUITableView,heighForRowAtIndex 没有返回我想要的高度
【发布时间】:2014-04-17 13:56:12
【问题描述】:

我已经实现了以下内容:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ return 75; }

只有在接收到 json 序列化数据时,高度才会正确显示。当它没有获取数据时,它将高度显示为默认高度。什么覆盖了这个方法?

【问题讨论】:

  • 您需要设置表格视图的rowHeight 属性(在代码中或在IB 中)。如果不设置,根据文档获取默认高度:If you do not explicitly set the row height, UITableView sets it to a standard value.

标签: ios uitableview height


【解决方案1】:

如果您希望每一行的高度相同,请不要使用tableView:heightForRowAtIndexPath: 方法。

改为在viewDidLoad中设置一次表格视图行高:

self.tableView.rowHeight = 75;

这适用于所有行,而不仅仅是包含数据的行。

【讨论】:

    【解决方案2】:

    只有在 numberOfRowsInSection 返回值 > 0 时才调用 heightForRowAtIndexPath 方法。在这种情况下,您看到的是默认的空表视图。

    我认为在你没有数据的情况下,你应该通过viewDidLoad中的这行代码隐藏所有默认的空行:

    tableView.tableFooterView = [UIView new];
    

    并显示并清空图像标签,让用户知道没有返回数据。示例:

    - (void) requestData {
      // Your request data code
      // When request data complete, check and show that label
      self.labelNoData.hidden = (arrayData.count == 0);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 2021-04-07
      • 2013-06-28
      • 1970-01-01
      • 2020-05-10
      相关资源
      最近更新 更多