Here is a example for UItableview within scrollview using autolayout. It works well. But if I remove tableFooterView from UItableview, the UItableview is disppeared.
I want to know why tableview need a tableFooterView here? thank you.
the following is source code :
MyTableView.m
file:
@implementation MyTableView
- (CGSize)intrinsicContentSize {
[self layoutIfNeeded];
return CGSizeMake(UIViewNoIntrinsicMetric, self.contentSize.height);
}
@end
ViewController.m
file:
#import "ViewController.h"
#import "MyTableView.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@end
@implementation ViewController
- (void)loadView {
UIView *view = [[UIView alloc] init];
self.view = view;
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.backgroundColor = [UIColor cyanColor];
[view addSubview:scrollView];
UITableView *tableView = [[MyTableView alloc] init];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
tableView.dataSource = self;
tableView.delegate = self;
[scrollView addSubview:tableView];
//why have to need this ?
tableView.tableFooterView = [[UILabel alloc] init];
//add constraint
NSDictionary *views = NSDictionaryOfVariableBindings( scrollView, tableView );
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[scrollView]-30-|" options:0 metrics:nil views:views]];
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:nil views:views]];
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView]|" options:0 metrics:nil views:views]];
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[tableView]-0-|" options:0 metrics:nil views:views]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:tableView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];
return cell;
}
@end