Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

前端 未结 22 1397
长情又很酷
长情又很酷 2020-11-22 10:10

So I was making an rss reader for my school and finished the code. I ran the test and it gave me that error. Here is the code it\'s referring to:

- (UITableV         


        
22条回答
  •  借酒劲吻你
    2020-11-22 10:46

    I spent hours last night working out why my programmatically generated table crashed on [myTable setDataSource:self]; It was OK commenting out and popping up an empty table, but crashed every time I tried to reach the datasource;

    I had the delegation set up in the h file: @interface myViewController : UIViewController

    I had the data source code in my implementation and still BOOM!, crash every time! THANK YOU to "xxd" (nr 9): adding that line of code solved it for me! In fact I am launching a table from a IBAction button, so here is my full code:

        - (IBAction)tapButton:(id)sender {
    
        UIViewController* popoverContent = [[UIViewController alloc]init];
    
        UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];
        popoverView.backgroundColor = [UIColor greenColor];
        popoverContent.view = popoverView;
    
        //Add the table
        UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)         style:UITableViewStylePlain];
    
       // NEXT THE LINE THAT SAVED MY SANITY Without it the program built OK, but crashed when      tapping the button!
    
        [table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        table.delegate=self;
        [table setDataSource:self];
        [popoverView addSubview:table];
        popoverContent.contentSizeForViewInPopover =
        CGSizeMake(200, 300);
    
        //create a popover controller
        popoverController3 = [[UIPopoverController alloc]
                              initWithContentViewController:popoverContent];
        CGRect popRect = CGRectMake(self.tapButton.frame.origin.x,
                                    self.tapButton.frame.origin.y,
                                    self.tapButton.frame.size.width,
                                    self.tapButton.frame.size.height);
    
    
        [popoverController3 presentPopoverFromRect:popRect inView:self.view   permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    
    
    
       }
    
    
       #Table view data source in same m file
    
       - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
       {
        NSLog(@"Sections in table");
        // Return the number of sections.
        return 1;
       }
    
       - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
      {
        NSLog(@"Rows in table");
    
        // Return the number of rows in the section.
        return myArray.count;
       }
    
       - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
        {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
        NSString *myValue;
    
        //This is just some test array I created:
        myValue=[myArray objectAtIndex:indexPath.row];
    
        cell.textLabel.text=myValue;
        UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 12.0 ];
        cell.textLabel.font  = myFont;
    
        return cell;
       }
    

    By the way: the button must be linked up with as an IBAction and as a IBOutlet if you want to anchor the popover to it.

    UIPopoverController *popoverController3 is declared in the H file directly after @interface between {}

提交回复
热议问题