简体   繁体   English

滚动时UITableView崩溃

[英]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 tableview崩溃错误消息

[__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. 假设您使用的是ARC,则将释放popoverView对象。 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! 您几乎应该永远不要实例化UIViewController,并且绝对不要将一个视图控制器的view实例分配给另一个实例!

Here's how I'd rewrite your titleAction: method: 这是我将重写您的titleAction:方法的方法:

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. 至少,UIPopoverController应该保持对titleViewController的强烈引用,并防止其被释放。

PS I changed your "self->popoverController" to self.popoverController as the -> is not really correct for what you are trying to do. PS我将您的“ self-> popoverController”更改为self.popoverController,因为->对于您要执行的操作并不正确。 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. viewDidLoad您分配并启动一个UITableView并将其连接到self。 You don't have a UITableView? 您没有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. 没有看到整个项目,我的猜测是您拥有UITableViews这是一个问题,但不一定是崩溃问题。 Did you inherit from UITableViewController as base class? 您是否从UITableViewController继承为基类?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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