2012-08-08 35 views
-1

我试图能够插入一个对象到我的表,但它崩溃,因为一个未捕获的异常,每当我点击添加按钮。它说问题与NSMutableArray和我的表

Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update 

这是我用来处理我所有的表的东西主视​​图控制器...

#import "MasterViewController.h" 

#import "DetailViewController.h" 

#define kFileName  @"steakNames.plist" 

@interface MasterViewController() { 
    NSMutableArray *_objects; 
} 
@end 

@implementation MasterViewController 

@synthesize steakName; 

- (NSString *)dataFilePath { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    return [documentsDirectory stringByAppendingPathComponent:kFileName]; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     self.title = NSLocalizedString(@"Master", @"Master"); 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    NSString *filePath = [self dataFilePath]; 
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { 
     _objects = [[NSMutableArray alloc] initWithContentsOfFile:filePath]; 
    } 

    UIApplication *app = [UIApplication sharedApplication]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app]; 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.navigationItem.leftBarButtonItem = self.editButtonItem; 

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)]; 
    self.navigationItem.rightBarButtonItem = addButton; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

- (void)insertNewObject:(id)sender 
{ 
    if (!steakName) { 
     steakName = @"No name"; 
    } 
    [_objects insertObject:[self steakName] atIndex:0]; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 

    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

#pragma mark - Table View 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return _objects.count; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 


    NSDate *object = [_objects objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [object description]; 
    return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [_objects removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
    } 
} 

/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (!self.detailViewController) { 
     self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; 
    } 
    NSDate *object = [_objects objectAtIndex:indexPath.row]; 
    self.detailViewController.detailItem = object; 
    [self.navigationController pushViewController:self.detailViewController animated:YES]; 
} 

- (void)applicationWillResignActive:(NSNotification *)notification { 
    [_objects writeToFile:[self dataFilePath] atomically:YES]; 
} 
@end 
+0

你也重写了'numberofSectionsInTableView'吗?你可以发布一些代码,当你点击添加按钮时会发生什么? – 2012-08-08 19:59:11

+0

你试图添加的代码在哪里? – 2012-08-08 20:02:29

+0

@DanF我确实把它放在那里,我把它设置为1. – 2012-08-08 20:59:58

回答

0

很可能是错误的原因是,它返回0:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    return ...; 
} 

我建议你首先用所有必需的方法完成一个完整的表视图实例,并从那里开始。

一般需要实现这些方法:

  • (NSInteger的)numberOfSectionsInTableView:(UITableView的*)的tableView

  • (的NSString *)的tableView:(UITableView的*)的tableView titleForHeaderInSection:(NSInteger的)部

  • (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分

  • (的UITableViewCell *)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath

  • (无效)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath

所以远远没有任何NSMutableArray的问题,无论如何,如果数组还没有对象的数量,就不能在索引处插入对象,例如,在索引4处插入,但数组只有2个对象。您始终可以将对象添加到NSMutableArray并增加此数组中的对象数。

+0

你甚至读过我的问题吗?我特别说过我在那里。 – 2012-08-08 20:59:03

+0

你检查过这个方法返回什么吗?添加按钮到底做了什么?问题在于你不在这里展示的代码! – user387184 2012-08-08 21:17:15

+0

我更新了主帖,抱歉不够清楚。 – 2012-08-10 20:45:58