2012-08-08 71 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