EXC_BAD_ACCESS 以编程方式创建 table 视图时

EXC_BAD_ACCESS when creating table view programmatically

我正在尝试以编程方式创建一个视图,其中包含两个视图,一个用于搜索,另一个是将显示照片的表视图;

但是我遇到了 EXC_BAD_ACCESS 错误代码=2,所有控制器代码都在下面。我怀疑有一个无限循环,但不明白为什么。 感谢您的帮助...

@interface PhotosViewController () <UITableViewDataSource, UITableViewDelegate>
    @property (strong, nonatomic) UITableView *tableView;
    @end

    @implementation PhotosViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.title = @"Instagram";

        [self.tableView registerClass:[PhotoTableViewCell class] forCellReuseIdentifier:CellIdentifier];

        self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
        self.tableView.allowsSelection = NO;
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    - (void)loadView
    {
        _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];

        _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView reloadData];

        [self.view addSubview:_tableView];
    }

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 1;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        PhotoTableViewCell *cell = (PhotoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        // Configure the cell...
        [self configureCell:cell atIndexPath:indexPath];

        return cell;
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        //    NSLog(@"%f", self.navigationController.navigationBar.bounds.size.height);
        return /*tableView.bounds.size.height -self.navigationController.navigationBar.bounds.size.height -*/40.0;
    }

    #pragma mark - Helper Methods

    - (void)configureCell:(PhotoTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

        // configure photo cell
        if (cell == nil) {
            cell = [[PhotoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        cell.namelabel.text = @"Mister Tester";
        cell.dateLabel.text = @"2 hours ago";
    }

在loadView中你不应该先调用[super loadView]或者赋值self.view。

我相信阅读 self.view 没有它会导致无限循环。