【问题标题】:Show only the first row in a UITableView仅显示 UITableView 中的第一行
【发布时间】:2014-07-22 14:36:32
【问题描述】:

所以,大多数问题都是“我的 tableView 只显示第一行,怎么了?”。 好吧,我需要的恰恰相反。

我有一个SearchBar(不是Search Display Controller),在用户开始输入之前,我只想显示第一行,仅此而已。

我的TableView的内容是Dynamic Prototypes,有2个Prototype Cells。 第一个是我想要显示的唯一一个,但它显示其他空白。

https://www.dropbox.com/s/q4ak6z3gbc0gh5c/Screenshot%202014-07-22%2011.24.22.png

这是我的tableView:numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger numberOfRows = 0;

    if ([self.searchBar.text isEqualToString:@""]) {
        numberOfRows = 1;
    }

    return numberOfRows;
}

所有的帮助将不胜感激! \o/

【问题讨论】:

    标签: objective-c uitableview ios7 storyboard


    【解决方案1】:

    可能有更好的解决方案,但正如之前提出的那样:

    CGRect frame = [self.tableView frame];
    
    frame.size.height = [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    self.tableView.frame = frame;
    

    【讨论】:

      【解决方案2】:

      在视图控制器的-tableView:numberOfRowsInSection: 中确保tableView 等于self.tableView,然后返回1:

      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
          if (tableView == self.tableView) {
              return 1;
          } else {
              return yourDataModel.count;
          }
      }
      

      【讨论】:

      • 是的,我做了,但是你的截图只显示了一个模拟器,表格视图中只有一个单元格,完全可以通过这种方式实现。
      • @edmoks 如果您的意思是隐藏单元格之间的分隔符,请参阅以下问题:stackoverflow.com/questions/286332/…
      【解决方案3】:

      你必须在创建(alloc)时改变UITableView的contentView或frame的大小。

      此高度必须与默认(44px)或自定义的 UITableViewCell 相同。

      【讨论】:

        【解决方案4】:

        写下下面的 4 行来做一个只有一行的表格。如果您想扩展表格的内容,只需让表格视图可滚动即可。

        tblView                     = [[UITableView alloc]initWithFrame:CGRectZero];
        tblView.separatorColor      = [UIColor clearColor];
        tblView.tableFooterView     = [[UIView alloc] initWithFrame:CGRectZero];    
        tblView.scrollEnabled       = NO;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-08
          • 1970-01-01
          • 1970-01-01
          • 2011-11-13
          • 1970-01-01
          • 1970-01-01
          • 2023-04-01
          • 2016-09-08
          相关资源
          最近更新 更多