2015-06-28 2 views
0

Вот пример 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 
+0

Почему вы кладете TableView внутри Scrollview? – Petar

ответ

1

Это очень плохой подход. Не используйте uitableview внутри uiscrollView. Подробнее here

Вскоре

Вы не должны вставлять UIWebView или объекты UITableView в UIScrollView объектов. Если вы это сделаете, может возникнуть непредвиденное поведение, потому что событиядля двух объектов могут быть перемешаны и неправильно обработаны.

Таким образом, эта проблема относится к тому, что говорит Apple - неожиданное поведение

Вы можете переписать код, как, например:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
     UIView *view = [[UIView alloc] init]; 
     self.view = view; 

     UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame]; 
     tableView.translatesAutoresizingMaskIntoConstraints = NO; 
     tableView.dataSource = self; 
     tableView.delegate = self; 
     [view addSubview:tableView]; 
     //add constraint 
     NSDictionary *views = NSDictionaryOfVariableBindings( tableView); 

     [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; 
    } 

Если вы все еще хотите добавить UITableView - ваши ограничения выставиться неправильно. Вы должны добавить ограничения Tableview к UIScrollView, не UIView, а также добавить равное ограничение высоты в табличном

заменить ТРУДНОСТИ код:

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

    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView]|" options:0 metrics:nil views:views]]; 
    [scrollView 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]]; 
    [view addConstraint:[NSLayoutConstraint constraintWithItem:tableView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeHeight multiplier:1 constant:0]]; 
+0

спасибо за отличный ответ! Спасите меня. – stackFish

+0

@stackfisher, отметьте его как ответ, пожалуйста, если это было полезно для вас :) – Doro

Смежные вопросы