UITableView with NSLayoutConstraint returns null with runtime crash

UITableView with NSLayoutConstraint returns null with runtime crash

我有 2 个视图控制器 - TableContainer 和 TableView。 TableView 是 UIViewController 子类,符合 UITableViewDataSource 和 Delegate。 UITableView 是 TableView 控制器中的 属性。

我在 TableContainer 中初始化了 5 个 TableView 控制器,并将 tableView 属性添加为主 TableContainer 视图的子视图。

然后我使用 NSLayoutConstraint 进行布局以适应 TableContainer 视图中的 5 个 tableView。我遇到了这个崩溃

'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Constraint must contain a first layout item'

我已经记录了,原因是 tableView 属性 为空。但是,我 运行 与 TableView 控制器完全相同的代码不是作为子类 UIViewController 而是作为实际的 UITableViewController 并且它工作正常。

代码如下 TableView.h

#import <UIKit/UIKit.h>

@interface TableView : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;

TableView.m(省略委托和数据源方法,因为我已经测试过并且它们工作正常)

@interface TableView ()

@end

@implementation TableView

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView = [[UITableView alloc]init];
    self.tableView.userInteractionEnabled = FALSE;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView setShowsVerticalScrollIndicator:NO];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

}

TableContainer.h

@interface TableContainer : UIViewController

@property (strong, nonatomic) TableView *tvVC1;
@property (strong, nonatomic) TableView *tvVC2;
@property (strong, nonatomic) TableView *tvVC3;
@property (strong, nonatomic) TableView *tvVC4;
@property (strong, nonatomic) TableView *tvVC5;

@end

TableContainer.m(省略了 tvVC 2-5 的约束代码,因为它们与 tvVC1 的相同。)

@implementation TableContainer

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tvVC1 = [[TableView alloc]init];
    [self.view addSubview:self.tvVC1.tableView];

    self.tvVC2 = [[TableView alloc]init];
    [self.view addSubview:self.tvVC2.tableView];

    self.tvVC3 = [[TableView alloc]init];
    [self.view addSubview:self.tvVC3.tableView];

    self.tvVC4 = [[TableView alloc]init];
    [self.view addSubview:self.tvVC4.tableView];

    self.tvVC5 = [[TableView alloc]init];
    [self.view addSubview:self.tvVC5.tableView];

}


-(void)viewDidAppear{

        UILayoutGuide *layoutGuide = self.view.layoutMarginsGuide;


        self.tvVC1.tableView.translatesAutoresizingMaskIntoConstraints = FALSE;
        NSLayoutConstraint *vc1 = [NSLayoutConstraint constraintWithItem:self.tvVC1.tableView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:0.2f constant:0.0f];
        vc1.active = TRUE;
        [self.tvVC1.tableView.leadingAnchor constraintEqualToAnchor:layoutGuide.leadingAnchor].active = TRUE;
        [self.tvVC1.tableView.topAnchor constraintEqualToAnchor:layoutGuide.topAnchor].active = TRUE;
        [self.tvVC1.tableView.bottomAnchor constraintEqualToAnchor:layoutGuide.bottomAnchor].active = TRUE;

您还没有让子 table 视图控制器 load/create 他们的视图。您需要访问每个子视图控制器的 view 属性,而不仅仅是 tableView 属性。