简体   繁体   中英

UITableView Crashes when scrolling

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSLog(@"Returning num sections");
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"Returning num rows");
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Trying to return cell");
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:CellIdentifier] ;
    }
    cell.textLabel.text = @"Hello";
    NSLog(@"Returning cell");
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Selected row");
}


- (void)viewDidLoad
{
    [super viewDidLoad];

   m_titleTable = [[UITableView alloc] init] ;
    m_titleTable.dataSource = self;
    m_titleTable.delegate = self;
    m_titleTable.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:m_titleTable];
    // Do any additional setup after loading the view from its nib.
}

-(void)titleAction:(id)sender{
    NSLog(@"Calling");


    UIViewController* popoverContent = [[UIViewController alloc]
                                        init];
    TitleViewController *popoverView = [[TitleViewController alloc]initWithNibName:@"TitleViewController" bundle:nil];
    popoverContent.view = popoverView.view;

    //resize the popover view shown
    //in the current view to the view's size
    popoverContent.contentSizeForViewInPopover = CGSizeMake(150, 150);
    //create a popover controller
    self->popoverController = [[UIPopoverController alloc]
                              initWithContentViewController:popoverContent];
    //present the popover view non-modal with a
    //refrence to the button pressed within the current view
    [self->popoverController presentPopoverFromRect:btn_title.frame
                                            inView:self.view
                          permittedArrowDirections:UIPopoverArrowDirectionUp
                                          animated:YES];

}

The tableview Crash error message

[__NSCFType tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x6e7dca0
2012-08-28 14:17:10.539 Demo[2790:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x6e7dca0'

Your problem is that your view controller is being deallocated. You shouldn't be doing something like this:

UIViewController* popoverContent = [[UIViewController alloc]
                                    init];
TitleViewController *popoverView = [[TitleViewController alloc]initWithNibName:@"TitleViewController" bundle:nil];
popoverContent.view = popoverView.view;

Assuming you are using ARC, your popoverView object will be deallocated. Also this is just incorrect. You should almost never instantiate a UIViewController, and definitely never assign one view controller's view instance to another ones!

Here's how I'd rewrite your titleAction: method:

TitleViewController *titleViewController = [[TitleViewController alloc]initWithNibName:@"TitleViewController" bundle:nil];

//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(150, 150);
//create a popover controller
self.popoverController = [[UIPopoverController alloc]
                          initWithContentViewController:titleViewController];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[self.popoverController presentPopoverFromRect:btn_title.frame
                                        inView:self.view
                      permittedArrowDirections:UIPopoverArrowDirectionUp
                                      animated:YES];

The UIPopoverController, at least, should keep a strong reference to the titleViewController and prevent it from being deallocated.

PS I changed your "self->popoverController" to self.popoverController as the -> is not really correct for what you are trying to do. Dot notation makes explicit that you are setting a property on an object.

in viewDidLoad you allocates and initiates a UITableView and connect it to self. You don't have a UITableView? defined and connect in the nib? Without seeing your whole project my guess is that you have UITableViews - this is a problem but not necessarily the crash problem. Did you inherit from UITableViewController as base class?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM