簡體   English   中英

表格視圖中的“ EXEC_BAD_ACCESS”,“ [[TasksPageViewController tableView:numberOfRowsInSection:]”

[英]'EXEC_BAD_ACCESS' in Table View, “[TasksPageViewController tableView:numberOfRowsInSection:]”

我已經嘗試解決這一錯誤的參考資料已有2天了,但是我找不到解決方法。

我的應用程序是一個基本的待辦事項列表,其中包含用於不同類別的幾頁(UIPageControl),以及用於統計有關用戶已完成任務的第一頁。 (如果您有3個類別,則最終會有4頁)。

它從NSUserDefaults加載一系列類別,可以通過通過UIButton訪問的模式視圖修改該類別,就像Wheather應用中的(i)一樣。

頁面控件由ViewController類(它是主視圖)構建。 對於類別數組中的每個元素,視圖控制器都會生成一個新的TasksPageViewController類實例,並TasksPageViewController實例化一個情節TasksPageViewController視圖。

一切工作正常,直到我將UITableView放在情節TasksPageViewController視圖中並將其dataSource鏈接到同一類。

TasksPageViewController為UITableView dataSource實現所需的類后,它可以正常運行,但是如果我嘗試進入模態視圖以修改類別,則在按DONE時(它應該重新加載ViewController並為該頁面重新構建許多頁面) UIPageControll),則應用程序將因EXEC_BAD_ACCESS而崩潰。

使用NSZombiesEnabled進行調試,我得到了:

2012-02-12 18:55:49.460 Pontinhos [25601:fe03] * -[TasksPageViewController tableView:numberOfRowsInSection:]:已發送消息到已釋放實例0x68c3040

但是我真的不知道該如何進行,我不知道我正在調用哪個對象。

腳本如下:

ViewController.h

#import <UIKit/UIKit.h>
#import "MainPageViewController.h"
#import "TasksPageViewController.h"

@interface ViewController : UIViewController{

   IBOutlet UIScrollView *scrollView;
    IBOutlet UIPageControl *pageControl;
    NSMutableArray * views;

    // To be used when scrolls originate from the UIPageControl
    BOOL pageControlUsed;
}

@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
@property (nonatomic, retain) NSMutableArray *views;

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
- (IBAction)changePage;

@end

ViewController.m

#import "ViewController.h"

@implementation ViewController

@synthesize scrollView;
@synthesize pageControl;
@synthesize views;

//carrega views que estão na array.
- (void)viewDidLoad
{
    [super viewDidLoad];

    //LoadCategories
    NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
    NSMutableArray *categories = [[NSMutableArray alloc] initWithArray:[data objectForKey:@"categories"]];

    // MAIN PAGE
    MainPageViewController *mainpage = [self.storyboard instantiateViewControllerWithIdentifier:@"MainPageViewController"];
    CGRect mframe = scrollView.frame;
    mframe.origin.x = 0;
    mframe.origin.y = 0;
    mainpage.view.frame = mframe;

    [self.scrollView  addSubview:mainpage.view];

    int i = 1;

    NSLog(@"Iniciando criação de páginas!");

    for(id name in categories) {

        NSLog(@"Carregou página.%i",i);
        NSLog(@"categories count:%i",[categories count]);
        TasksPageViewController *tasks = [self.storyboard instantiateViewControllerWithIdentifier:@"TasksPageViewController"];

        CGRect frame = scrollView.frame;
        frame.origin.x = frame.size.width * i;
        frame.origin.y = 0;
        tasks.view.frame = frame;

        [tasks populateWithData:(i-1) categoryName:name];

        [scrollView addSubview:tasks.view];
        tasks = nil;
        i++;

    }

    self.pageControl.numberOfPages = [categories count]+1;
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * ([categories count]+1), self.scrollView.frame.size.height);

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (pageControlUsed)
    {
        // do nothing - the scroll was initiated from the page control, not the user dragging
        return;
    }

    // Update the page when more than 50% of the previous/next page is visible
    CGFloat pageWidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
}

- (IBAction)changePage {
    // update the scroll view to the appropriate page
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    [self.scrollView scrollRectToVisible:frame animated:YES];
    pageControlUsed = YES;
}


// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    pageControlUsed = NO;
}

// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    pageControlUsed = NO;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.scrollView = nil;
}

@end

TasksPageViewController.h

#import <UIKit/UIKit.h>
#import "TasksTableCellPrototypes.h"

@interface TasksPageViewController : UIViewController <UITableViewDataSource>{

    IBOutlet UILabel *categoryName;
    IBOutlet UITableView *tableView;
    //NSMutableArray *tasksData;

}

//@property (nonatomic,retain)  NSMutableArray *tasksData;
@property (nonatomic,retain)  UITableView *tableView;
@property (nonatomic,retain) UILabel *categoryName;
- (void) populateWithData:(int)dataId categoryName:(NSString *)name;

@end

TasksPageViewController.m

#import "TasksPageViewController.h"

@implementation TasksPageViewController

@synthesize tableView;
//@synthesize tasksData;
@synthesize categoryName;

- (void)viewDidLoad
{
    [super viewDidLoad];

   // NSUserDefaults *data = [NSUserDefaults standardUserDefaults];

    //tasksData = [[NSMutableArray alloc] initWithArray:[data objectForKey:@"tasks"]];

    //tasksData = [[NSMutableArray alloc] initWithObjects:@"lalal",@"lololo",nil];

}


- (void) populateWithData:(int)dataId categoryName:(NSString *)name {

    //self.categoryName.text = name;
}   

// Número de categorias

- (NSInteger)tableView:(UITableView *)tableViewnumberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"contou table view tasks");
    return 4;
}

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

    TasksTableCellPrototypes *cell = [[TasksTableCellPrototypes alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"taskCell"];
    return cell;

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

謝謝。

編輯:顯然ViewController不會保留對TasksPageViewController的引用,而UITableView找不到其dataSource。 我真的不知道如何使用ARC保留它。

在ViewController viewDidLoad ,當您遍歷類別時,將實例化一個名為tasks的TasksPageViewController,但是您不會在任何地方保留引用。 因此,稍后在發送tableView:numberOfRowsInSection:消息時找不到TasksPageViewController。

我看到在您的ViewController中,您合成了一個名為views的屬性,但是您在任何地方都不使用該屬性。 也許您應該重命名該屬性viewControllers ,然后在viewDidLoad循環的末尾,應該將tasks對象添加到viewControllers可變數組中,而不是將tasks設置為nil。 我認為這可以防止崩潰。

for(id name in categories) {

    TasksPageViewController *tasks = [self.storyboard instantiateViewControllerWithIdentifier:@"TasksPageViewController"];

    ... 

    [scrollView addSubview:tasks.view];
    [self.viewControllers addObject:tasks];
    i++;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM