how to hide empty rows in a UITableView and change the height of the Uitableview based on non-empty rows

前端 未结 11 1866
离开以前
离开以前 2021-01-30 05:06

I have couple of problems with my UITableView.

  1. When I add a UITableview on my page, by default it brings up some fixed number of rows,

相关标签:
11条回答
  • 2021-01-30 05:25

    I solved the problem by creating a simple UIView as footer view with the same background color as the table background:

    (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
        UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)] autorelease];
        view.backgroundColor = [UIColor whiteColor];
        return view;
    }
    

    Maybe you have to disable scrolling in your table view in addition.

    0 讨论(0)
  • 2021-01-30 05:27
    - (void) viewDidLoad
    {
      [super viewDidLoad];
      self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
    }
    
    0 讨论(0)
  • 2021-01-30 05:28

    NEW ANSWER

    In Swift 2.2, 3.0 and onwards, do the following:

    tableView.tableFooterView = UIView()

    OLD ANSWER BELOW. KEPT FOR POSTERITY.

    If you must use UITableViewStylePlain, and you don't use a footerView for anything else, you can use the following semi-dirty solution if you have ARC enabled.:

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    {
        UIView *view = [[UIView alloc] init];
    
        return view;
    }
    

    If you have ARC disabled, use the following:

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    {
        UIView *view = [[[UIView alloc] init] autorelease];
    
        return view;
    }
    

    This creates an invisible footerView, that appears immediately after the last data-filled cell.

    0 讨论(0)
  • 2021-01-30 05:31

    If your tableview has multiple sections, you may try using this:

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    {
        if (section == tableView.numberOfSections - 1) {
            return [UIView new];
        }
        return nil;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
        if (section == tableView.numberOfSections - 1) {
            return 1;
        }
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-30 05:34

    For Swift:

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

    For C#: (Please don't forget to add using CoreGraphics; )

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);
        this.sampleTableview.TableFooterView = new UIView(frame: CGRect.Empty);
    }
    
    0 讨论(0)
  • 2021-01-30 05:38
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 
     { 
         return 0.01f;
     }
    

    and for iOS 7

     self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    0 讨论(0)
提交回复
热议问题