2015-05-07 3 views
1

Я пытаюсь создать представление программно, которое будет иметь два вида внутри него, одно для поиска, а другое - табличное представление, которое будет показывать фотографии;EXC_BAD_ACCESS при создании табличного представления программно

Но у меня ошибка 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"; 
    } 
+0

В 'loadView' вы не должны сначала вызывать' [super loadView] '. Я считаю, что чтение self.view без этого вызывает бесконечный цикл. Также в этой функции вы не назначили self.view для представления, просто использовали его. –

+0

Большое вам спасибо, self.view = [UIView new] - это ответ ... –

+0

Fab. Отправленный в ответ, если вы так любезны принять. –

ответ

1

В loadView вы должны не называть [супер loadView] или назначить self.view первым.

Я считаю, что чтение self.view без этого может вызвать бесконечный цикл.

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