【问题标题】:Hide empty cells in UITableView隐藏 UITableView 中的空单元格
【发布时间】:2010-07-14 17:09:49
【问题描述】:

我有一个可搜索的表格视图,但只想显示用户开始搜索后返回的单元格数。例如,如果用户键入一个字符并且只有一个单元格与搜索匹配,则其余单元格和表格的其余单元格都是空的。我想知道是否有办法隐藏那些空单元格。我还想知道如果一个人的搜索不匹配任何数据,是否有办法隐藏表格视图(默认情况下,它会显示一个空单元格表格,上面写着“无结果”)。

谢谢。

【问题讨论】:

    标签: iphone objective-c uitableview uisearchbar


    【解决方案1】:

    将tableFooterView 设置为一些虚拟视图([[UIView new] autorelease] 可以正常工作)。这会提示 UITableView 显示页脚(大小为零)来代替空的“单元格”。

    【讨论】:

    • @codejunkie - 我没试过;应该可以正常工作,但您不需要自动释放。
    • @codejunkie - 如果您使用 ARC,则不会自动发布。只需使用 [UIView new]
    • @TomSwift 好的,我看到它隐藏了默认由视图创建的单元格“但是”它并没有隐藏我的主要问题,因为我有一些可以选择但没有内容的空单元格我该如何隐藏他们?我为此提出了一个新问题。
    • 这里提出的问题:stackoverflow.com/questions/9859427/…
    【解决方案2】:
    - (void) viewDidLoad
    {
      [super viewDidLoad];
     *// self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];*   
      *//Since memory management is controlled by ARC now, so ignore autorelease*
      self.tableView.tableFooterView = [[UIView alloc] init] ;
    }
    

    【讨论】:

    • self.tableView.tableFooterView = [[UIView alloc] init];工作,但我在自动释放时遇到错误(自动释放不可用:在自动引用计数模式下不可用)
    • 我已经更新了答案,希望它更容易理解。
    【解决方案3】:

    更改数据源中tableView:numberOfRowsInSection: 的实现,以返回搜索结果的数量而不是项目的总数。然后用搜索结果而不是其余数据填充这些单元格。

    【讨论】:

      【解决方案4】:

      很简单,你可以用这个..

      tableView.tableFooterView = [UIView new];
      

      【讨论】:

        【解决方案5】:

        对于 swift 2.x xCode 7:

        override func viewDidLoad() {
                super.viewDidLoad()
                print("∙ \(NSStringFromClass(self.dynamicType))")
                ...
                self.tableView.tableFooterView = UIView()
        }
        

        【讨论】:

          【解决方案6】:
          - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
          {
                 if (([array count]*65) > [UIScreen mainScreen].bounds.size.height - 66)
                  {
                    Table.frame = CGRectMake(0, 66, self.view.frame.size.width, [array count]*65)); 
                  }else
                 {
                   Table.frame = CGRectMake(0, 66, self.view.frame.size.width, [UIScreen mainScreen].bounds.size.height - 66);
                  }
                  return [array count];
          }
          

          这里65是单元格的高度,66是UIViewController中导航栏的高度。

          【讨论】:

            【解决方案7】:

            在 Swift 中隐藏一个空单元格

            Swift 3 版

             override func viewWillAppear(_ animated: Bool) {
                super.viewWillAppear(animated)
            
                self.tableView.tableFooterView = UIView(frame: CGRect.zero)
            }
            

            Swift 2 版

             override func viewWillAppear(animated: Bool) {
                super.viewWillAppear(animated)
            
                self.tableView.tableFooterView = UIView(frame: CGRectZero)
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-08-19
              • 2014-12-13
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-12-11
              • 1970-01-01
              相关资源
              最近更新 更多