views:

32

answers:

1

below test code causes EXC_BAD_ACCESS when it's scrolled many time. would somebody like to tell me the problem?

-myview.h---------------------------------------------------

@interface MyView : UINavigationController < UITableViewDataSource, UITableViewDelegate, UINavigationBarDelegate >
{
   UITableView* mTableView;
   NSMutableArray* data;
}

-myview.m---------------------------------------------------

@implementation MyView

- (void)viewDidLoad
{
    [super viewDidLoad];
    int th = self.navigationBar.frame.size.height;
    int w = self.view.frame.size.width;
    int h = self.view.frame.size.height;

    mTableView = [[[UITableView alloc] initWithFrame:
                CGRectMake(0.0f, th, w, h - th) style:UITableViewStylePlain] retain];
    mTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth |
                                      UIViewAutoresizingFlexibleHeight;
    mTableView.dataSource = self;
    mTableView.delegate = self;
    [mTableView setRowHeight:55];

    [self.view addSubview:mTableView];

    data = [[[NSMutableArray alloc] init] retain];

    for (int i = 0; i < 150; i++)
    {

        [data addObject:[NSDictionary
        dictionaryWithObjects:[NSArray arrayWithObjects:@"good", [UIColor blackColor], nil]

        forKeys:[NSArray  arrayWithObjects:@"name", @"color", nil]]];
    }
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 150;
}

- (UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier
{
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    cell.textLabel.text = @"goooood";
    return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil)
        cell = [self tableViewCellWithReuseIdentifier:CellIdentifier];

  return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}


- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)dealloc
{
    [super dealloc];
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

@end
A: 

You should have a view controller that contains your table view (it could even be a subclass of UITableViewController). I don't believe that UINavigationController is mean't to be subclassed as you have. If you want a navigation controller you should create an instance of CustomTableViewController, containing your tableview, and then use the initWithRootViewController: method of UINavigationController:

CustomTableViewController * ctvc = [[CustomTableViewController alloc] init];
UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:ctvc];
[ctvc release];

// Superview is where you want this added 
// probably your window in app did finish launching
[superview addSubview:navigationController.view]; 

[navigationController release];
thelaws