首页 文章

如何在UITableView中删除空单元格? [重复]

提问于
浏览
331

这个问题在这里已有答案:

我试图用一些数据显示一个简单的 UITableView . 我希望设置 UITableView 的静态高度,以便它不会在表的末尾显示空单元格 . 我怎么做?

码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"%d", [arr count]);
    return [arr count];
}

10 回答

  • 2
    override func viewWillAppear(animated: Bool) {
        self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect)
    
    /// OR 
    
        self.tableView.tableFooterView = UIView()
    }
    
  • 14

    设置零高度表页脚视图(可能在您的 viewDidLoad 方法中),如下所示:

    Swift:

    tableView.tableFooterView = UIView()
    

    Objective-C:

    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    

    因为该表认为有一个页脚显示,它不会显示任何超出您明确要求的单元格 .

    Interface builder pro-tip:

    如果您使用的是 xib/Storyboard ,则可以将UIView(高度为0pt)拖到UITableView的底部 .

  • 1

    Swift 3 syntax:

    tableView.tableFooterView = UIView(frame: .zero)
    

    Swift syntax: < 2.0

    tableView.tableFooterView = UIView(frame: CGRect.zeroRect)
    

    Swift 2.0 syntax:

    tableView.tableFooterView = UIView(frame: CGRect.zero)
    
  • 6

    在故事板中,选择 UITableView ,并将属性样式从 Plain 修改为 Grouped .

  • 835

    在Xcode 6.1上使用swift实现

    self.tableView.tableFooterView = UIView(frame: CGRectZero)
    self.tableView.tableFooterView?.hidden = true
    

    第二行代码不会对演示文稿产生任何影响,您可以使用它来检查是否隐藏 .

    从这个链接中回答Fail to hide empty cells in UITableView Swift

  • 58

    我现在无法添加评论,因此将其添加为答案 .

    @Andy的回答很好,使用以下代码行可以获得相同的结果:

    tableView.tableFooterView = [UIView new];
    

    'new'方法属于 NSObject 类,并为 UIView 调用 allocinit 方法 .

  • 97

    我试过这段代码:

    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    

    viewDidLoad 部分,xcode6显示了警告 . 我已经把"self."放在它前面,现在它工作正常 . 所以我使用的工作代码是:

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
  • 1

    或者您可以调用tableView方法将页脚高度设置为1磅,它将添加最后一行,但您也可以通过设置页脚背景颜色来隐藏它 .

    码:

    func tableView(tableView: UITableView,heightForFooterInSection section: Int) -> CGFloat {
         return 1
    }
    

    looks like last line

  • 0

    Using UITableViewController

    接受的解决方案将改变 TableViewCell 的高度 . 要解决此问题,请执行以下步骤:

    • ViewDidLoad 方法中编写下面给出的代码片段 .

    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    • TableViewClass.m 文件中添加以下方法 .
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        return (cell height set on storyboard); 
    }
    

    而已 . 您可以构建和运行项目 .

  • 11

    在以下方法中:

    - (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中导航栏的高度 .

相关问题