我收到一个错误:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。
因为我将一个对象添加到一个数组的实例中,然后从另一个应该是同一个数组的实例中重新加载表视图。如何创建数组的单个实例并将其从一个类传递到另一个类?我可以在 java 中轻松地做到这一点,但在 Objective-C 中你不能制作静态变量,所以我不确定如何做到这一点。
编辑:更多代码。
这是另一个类中的一个方法,它被调用以保存文件。我正在使用 Core Data,所以首先它将文件添加到上下文(模型)中,然后添加到数组中,然后保存上下文。此方法位于名为“Player”的类中
-(BOOL)saveRecording {
Bank *B = [MusikerViewController daBank];
AudioTableViewController *ATVC2 = [MusikerViewController ATVControl];
NSLog(@"Place A");
AudioFile *myAudioFileMusicX314 = [[B addAudioFileEntityToModelWithDate:myDate andURLString:strPath] retain];
NSLog(@"Place B");
myAudioFileMusicX314.type = true;
[ATVC2 addAudioEntityToArray:myAudioFileMusicX314];
NSLog(@"Place C ***********************************************");
if(![B saveContext]) { //save context after adding file to keep consistancy
NSLog(@"addAudioFileEntityToModel is returning a nil managedObjectContext");
return NO;
}
NSLog(@"Place D");
[myDate release];
[strPath release];
[myAudioFileMusicX314 release];
[ATVC2 release];
NSLog(@"Place E");
return YES;
}
以下方法在包含表格视图的类中——它的名称为 AudioTableViewController
-(void)addAudioEntityToArray:(AudioFile *)event {
NSIndexPath *indexPath;
if(event.type) {
[[MusikerViewController recordingsArray] addObject:event];//self?
indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
}
else {
[[MusikerViewController downloadsArray] addObject:event];
indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
}
[[self tableView] setEditing:YES animated:NO];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
以下方法将对象添加到我的模型中
- (AudioFile *)addAudioFileEntityToModelWithDate:(NSDate *)theD andURLString:(NSString *)str {
NSLog(@"addAudio...WithDate -- called");
sound = (AudioFile *)[NSEntityDescription insertNewObjectForEntityForName:@"AudioFile" inManagedObjectContext:managedObjectContext];
sound.creationDate = theD;
sound.soundPath = str; //set the sound path to the sound file's url
[self alertForTitle];
NSLog(@"No problems yet at place D - addaudio...String, sound title is %@", sound.title);
NSLog(@"Context at addAudioFileEntityToModel is: %@", managedObjectContext);
return sound;
}
这是 MusikViewController.h 的重要部分——它跟踪recordingsArray和downloadsArray
@interface MusikerViewController : UIViewController {
}
NSMutableArray *recordingsArray;
NSMutableArray *downloadsArray;
+ (NSMutableArray *)recordingsArray;
+ (NSMutableArray *)downloadsArray;
和 MusikViewController.m
+ (NSMutableArray *)recordingsArray {
NSLog(@"recordingsArray called");
if(!recordingsArray) {
recordingsArray = [[NSMutableArray alloc] init];
NSMutableArray *bigTempArray = [[[[Bank alloc] init] autorelease] getFetchArray]; //change this
for(AudioFile *af in bigTempArray)
if(af.type) {
[recordingsArray addObject:af];
}
NSLog(@"recordingsArray exists");
}
return recordingsArray;
}
+ (NSMutableArray *)downloadsArray {
NSLog(@"recordingsArray called");
if(!downloadsArray) {
downloadsArray = [[NSMutableArray alloc] init];
// if(!bigTempArray)
NSMutableArray *bigTempArray = [[[[Bank alloc] init] autorelease] getFetchArray];
for(AudioFile *af in bigTempArray)
if(!af.type) {
[downloadsArray addObject:af];
}
}
return downloadsArray;
}
和一些 AudioTableViewController 方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(@"Place F");
if(section == 0) {
return [[MusikerViewController recordingsArray] count];
}
else if (section == 1) {
return [[MusikerViewController downloadsArray] count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
AudioFile *event;
if(indexPath.section == 0) {
event = (AudioFile *)[[MusikerViewController recordingsArray] objectAtIndex:indexPath.row];
} else if (indexPath.section == 1) {
NSLog(@"downAry indexPath caled at cellForRow...Path");
event = (AudioFile *)[[MusikerViewController downloadsArray] objectAtIndex:indexPath.row];
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
if(event.title) {
cell.detailTextLabel.text = [Player dateString:event.creationDate];
cell.textLabel.text = event.title;
} else {
cell.textLabel.text = [Player dateString:event.creationDate];
cell.detailTextLabel.text = nil;
}
return cell;
}
- (void)viewDidLoad //viewDidLoad for AudioTableViewController
{
[[self tableView] reloadData];
NSLog(@"viewDidLoad called for AudioTableViewController");
[super viewDidLoad];
self.title = @"Audio Files";//put this in application delegate
// Set up the buttons.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}