【问题标题】:UITableView - How to show rows only which contains dataUITableView - 如何仅显示包含数据的行
【发布时间】:2011-09-18 14:54:22
【问题描述】:

我怎样才能显示UITableView 仅包含数据而不包含其他行的行。默认情况下,UITableView 显示 10 行。如果我们有三行的数据它将只显示三行 我该如何实施?谢谢。

【问题讨论】:

标签: iphone objective-c ios ipad uitableview


【解决方案1】:

如果需要,您可以设置页脚视图并使其不可见。分隔符只会出现在包含数据的行中:

self.theTableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)] autorelease];

【讨论】:

    【解决方案2】:

    试试这个..

    self.tableView.tableFooterView = [UIView new];
    

    【讨论】:

      【解决方案3】:

      我认为不可能使用标准方式隐藏额外的分隔符。此行为是预设的。

      Plain 样式的 tableView 显示 row/separator 即使没有真正的行存在。只有 Grouped 样式的 tableView 只显示现有行。

      编辑:根据其他人的建议,您可以向 tableView 添加页脚视图以隐藏那些额外的分隔符。

      【讨论】:

      【解决方案4】:

      好的,我假设您正在使用 Interface Builder。

      简单的方法:

      1. 确保您已将视图控制器类选择为 UITableViewDelegate 和 UITabbleViewDataSource。

      2. 在 ViewController.h(或其他任何名称)中,在 @interface 定义的 { 之前添加 <UITableViewDelegate,UITableViewDataSource>。它必须符合那些协议的类。

      3. 确保最小的数据源协议方法在类中:

        – tableView:cellForRowAtIndexPath:  
        – tableView:numberOfRowsInSection: 
        
      4. 在-tableView:numberOfRowsInSection:方法中返回想要的行数-3或者通过一段代码来计算这个属性

      5. 在–tableView:cellForRowAtIndexPath: 方法中对单元格执行您需要的操作并返回它。样板代码

        是:

        -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
        {
            UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
        
         if (cell == nil) 
         {
             cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease];
         }
        
        cell.text = @"I am a cell";
        return cell;
        }
        

      注意如果您不使用 Interface Builder,请执行以下操作: tableView.delegate = self; tableView.datasource = self;

      【讨论】:

        【解决方案5】:

        添加一个填充屏幕其余部分的静态单元格。

        【讨论】:

          【解决方案6】:

          只需在 viewDidLoad() 方法中添加以下代码即可。

          self.tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
          

          【讨论】:

            【解决方案7】:

            您可以在以下函数的返回参数中指定所需的行数:

            - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
            
                return NUMBER_OF_ROWS;
            }
            

            更多详情请查看Apple的UITableView文档。

            http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html

            【讨论】:

            • 如果“data”是你的数组/字典,你可以返回[data count];
            猜你喜欢
            • 1970-01-01
            • 2011-12-08
            • 2013-02-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-10
            • 2015-11-05
            • 1970-01-01
            相关资源
            最近更新 更多