衫裤跑路
2013-01-25 10:45
采纳率: 0%
浏览 311

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

This question already has an answer here:

i am trying to display a simple UITableView with some data. I wish to set the static height of the UITableView so that it doesn't displays empty cells at the end of the table. how do I do that?

code:

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

转载于:https://stackoverflow.com/questions/14520185/how-to-remove-empty-cells-in-uitableview

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

11条回答 默认 最新

  • local-host 2013-01-25 11:11
    已采纳

    Set a zero height table footer view (perhaps in your viewDidLoad method), like so:

    Swift:

    tableView.tableFooterView = UIView()
    

    Objective-C:

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

    Because the table thinks there is a footer to show, it doesn't display any cells beyond those you explicitly asked for.

    Interface builder pro-tip:

    If you are using a xib/Storyboard, you can just drag a UIView (with height 0pt) onto the bottom of the UITableView.

    已采纳该答案
    打赏 评论
  • elliott.david 2013-12-31 12:24

    I can not add comment as of now so adding this as an answer.

    @Andy's answer is good and the same results can be achieved with the following line of code:

    tableView.tableFooterView = [UIView new];
    

    'new' method belongs to NSObject class and invokes alloc and init methods for UIView.

    打赏 评论
  • ~Onlooker 2014-04-18 10:17

    In the Storyboard, select the UITableView, and modify the property Style from Plain to Grouped.

    打赏 评论
  • from.. 2014-10-21 20:00

    Implemented with swift on Xcode 6.1

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

    The second line of code does not cause any effect on presentation, you can use to check if is hidden or not.

    Answer taken from this link Fail to hide empty cells in UITableView Swift

    打赏 评论
  • 七度&光 2014-10-30 14:29

    I tried the code:

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

    In the viewDidLoad section and xcode6 showed a warning. I have put a "self." in front of it and now it works fine. so the working code I use is:

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    打赏 评论
  • 撒拉嘿哟木头 2015-01-30 20:50

    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)
    
    打赏 评论
  • 笑故挽风 2015-10-30 05:41
    override func viewWillAppear(animated: Bool) {
        self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect)
    
    /// OR 
    
        self.tableView.tableFooterView = UIView()
    }
    
    打赏 评论
  • larry*wei 2015-12-08 12:59

    Using UITableViewController

    The solution accepted will change the height of the TableViewCell. To fix that, perform following steps:

    1. Write code snippet given below in ViewDidLoad method.

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

    2. Add following method in the TableViewClass.m file.

      - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
      {
          return (cell height set on storyboard); 
      }
      

    That's it. You can build and run your project.

    打赏 评论
  • 妄徒之命 2016-01-08 06:59

    in the below method:

    - (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];
    }
    

    here 65 is the height of the cell and 66 is the height of the navigation bar in UIViewController.

    打赏 评论
  • perhaps? 2016-01-08 07:03

    in viewDidLoad:

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    打赏 评论
  • ~Onlooker 2016-02-29 10:05

    or you can call tableView method to set the footer height in 1 point, and it will add an last line, but you can hide it too, by setting footer background color.

    code:

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

    looks like last line

    打赏 评论

相关推荐 更多相似问题