Вот пример UItableview в scrollview с использованием автозапуска. Это работает хорошо. Но если я удалю tableFooterView из UItableview, появится UItableview.Tableview не появился до установки tableFooterView
Я хочу знать, почему tableview нужен tableFooterView здесь? Спасибо.
следующее исходный код:
MyTableView.m
файл:
@implementation MyTableView
- (CGSize)intrinsicContentSize {
[self layoutIfNeeded];
return CGSizeMake(UIViewNoIntrinsicMetric, self.contentSize.height);
}
@end
ViewController.m
файл:
#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
Почему вы кладете TableView внутри Scrollview? – Petar