UITableView不更新

我正在使用核心数据模型和UITableViewController表视图。 我的模型似乎工作得很好,但是当我添加一个实体到我的模型时,我的表格视图没有更新。 我相信我的模型正在工作的原因是因为当我添加一个实体在运行时没有在视图中显示,但是如果我切割任务然后启动它,突然之前创build的实体显示在表视图中。

这里是我的表视图控制器 – AudioTableViewController.h

#import <UIKit/UIKit.h> #import "Bank.h" #import "Player.h" @class Player; @interface AudioTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> { Player *myMainMan; } -(void)addAudioEntityToArray:(AudioFile *)event; -(NSMutableArray *)recordingsArray; @end 

AudioTableViewController.m(实现文件)

 #import "AudioTableViewController.h" @implementation 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]; } - (void)viewDidLoad { [[self tableView] reloadData]; [super viewDidLoad]; self.title = @"Audio Files";//put this in application delegate self.navigationItem.leftBarButtonItem = self.editButtonItem; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. NSLog(@"Place I"); if(section == 0) { return [[MusikerViewController recordingsArray] count]; } else if (section == 1) { return [[MusikerViewController downloadsArray] count]; } } 

…更多的方法

这是我的Bank类的一部分,可能是相关的Bank.h

 #import <Foundation/Foundation.h> #import <AudioToolbox/AudioToolbox.h> #import "AudioFile.h" #import "AudioTableViewController.h" #import "MusikerAppDelegate.h" @interface Bank : NSObject <UIAlertViewDelegate> { NSManagedObjectContext *managedObjectContext; AudioFile *sound; } @property (retain, nonatomic) NSManagedObjectContext *managedObjectContext; +(NSString *)getDataPath:(NSString *)fileExtDate; -(AudioFile *)addAudioFileEntityToModelWithDate:(NSDate *)theD andURLString:(NSString *)str; -(BOOL)removeAudioFromModel:(id)audio; -(NSMutableArray *)getFetchArray; @end 

Bank.m

 #import "Bank.h" @implementation Bank @synthesize managedObjectContext; - (AudioFile *)addAudioFileEntityToModelWithDate:(NSDate *)theD andURLString:(NSString *)str { 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]; return sound; } - (BOOL)saveContext { NSError *error = nil; if(!managedObjectContext) { NSLog(@"managedObejctContext problem at saveContext Bank"); } if (![managedObjectContext save:&error]) { return NO; } else { return YES; } } - (NSMutableArray *)getFetchArray { NSLog(@"ManagedObjectContext at getFetchArray"); NSLog(@"Context: %@",managedObjectContext); NSFetchRequest *request = [[NSFetchRequest alloc] init]; if(!managedObjectContext) { NSLog(@"There is no managedObjectContext in getFetchArray Bank"); } NSEntityDescription *entity = [NSEntityDescription entityForName:@"AudioFile" inManagedObjectContext:managedObjectContext]; [request setEntity:entity]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [request setSortDescriptors:sortDescriptors]; [sortDescriptors release]; [sortDescriptor release]; NSError *error = nil; NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; if (mutableFetchResults == nil) { NSLog(@"mutableFetchResults array is nil"); } [request release]; return mutableFetchResults; } + (NSString *)getDataPath:(NSString *)fileExtDate { NSLog(@"getDataPath called"); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Musik Directory"]; //changing the recording directory to Musik Directory NSError *error; if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory]) { [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&error]; NSLog(@"In the if statement"); } NSString *docPath = [documentsDirectory stringByAppendingPathComponent:fileExtDate]; return docPath; } 

Player.h

 #import <Foundation/Foundation.h> #import <AVFoundation/AVFoundation.h> #import "Bank.h" @class Bank; @interface Player : NSObject <AVAudioPlayerDelegate, AVAudioRecorderDelegate> { Bank *daBank; AVAudioPlayer *musicPlayer; AVAudioRecorder *musicRecorder; NSDate *myDate; NSString *strPath; NSString *documentPath_; NSMutableArray *arrayListOfRecordSound; } -(void)playSoundFile:(NSString *)soundFilePath; -(BOOL)saveRecordingWithDate:(NSDate *)theD andURLString:(NSString *)str; -(void)recordSomething; -(void)playRecording; -(void)play; + (NSString *)dateString:(NSDate *)date; + (NSString *)dateString; @end 

Player.m

 - (Bank *)daBank { if(!daBank) { daBank = [[Bank alloc] init]; } return daBank; } -(BOOL)saveRecording { //this is the method that adds the new object to both the //model and view, the method that calls this method is not shown Bank *B = [MusikerViewController daBank]; AudioTableViewController *ATVC2 = [MusikerViewController ATVControl]; AudioFile *myAudioFileMusicX314 = [[B addAudioFileEntityToModelWithDate:myDate andURLString:strPath] retain]; myAudioFileMusicX314.type = true; [ATVC2 addAudioEntityToArray:myAudioFileMusicX314]; if(![B saveContext]) { NSLog(@"addAudioFileEntityToModel is returning a nil managedObjectContext"); return NO; } [myDate release]; [strPath release]; [myAudioFileMusicX314 release]; [ATVC2 release]; return YES; } 

最后但并非最不重要的,MusikViewController.h

 #import <UIKit/UIKit.h> #import "Player.h" #import "Bank.h" #import <QuartzCore/QuartzCore.h> #import "MyButtonView.h" #import "AudioTableViewController.h" @class AudioTableViewController; @class Bank; @class Player; @interface MusikerViewController : UIViewController { ] BOOL *pressed; } Player *myMainMan; Bank *daBank; AudioTableViewController *ATVControl; NSMutableArray *recordingsArray; NSMutableArray *downloadsArray; + (Bank *)daBank; + (Player *)myMainMan; + (AudioTableViewController *)ATVControl; + (NSMutableArray *)recordingsArray; + (NSMutableArray *)downloadsArray; @end 

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; } 

如果还有更多的方法可能是相关的,请让我知道,我会愉快地张贴他们。

我还应该提到,我可以在启动应用程序之后向arrays中添加一个实体,但在此之后它也会出现同样的问题。

我认为你的问题是你的视图控制器的体系结构。 尝试这个:

1)在应用程序委托中设置所有核心数据堆栈。 将相关的托pipe对象上下文作为属性传递给所有视图控制器。 这是苹果公司在许多示例代码示例中使用的模式。

2)对于每个表视图控制器,创build自己的 NSFetchedResultsController ,通常也作为一个属性。 使用苹果示例作为指导。

3) 不要从其他视图控制器获取任何数据,就像在代码中一样。 您不能也不应该从MusikerViewController获取AudioTableViewController的表数据。

有一个中央数据对象为查看控制器提供数据是可能的,有时也是有用的。 你不能让这个数据对象成为一个视图控制器。 这是一个公然(在你的情况下致命)违反MVCdevise模式。 你的错误很可能是由于(因为MusikerViewController不在屏幕上,甚至可能被释放)。

4)确保在你的datasource方法中, 使用提取的结果控制器来填充表格并传送媒体。

可能是你的 – (void)addAudioEntityToArray 🙁 AudioFile *)事件{}方法从辅助线程(MainUi线程除外)调用,以便直接重新加载表将不起作用。

反而尝试

 [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; 

希望能帮助到你!!

[self.tableView reloadData]添加到addAudioEntityToArray方法中。 这将强制UITableView在每次插入后从模型中重新加载。

你正在通过setter设置eventsArray:

 [self setEventsArray:[daBank getFetchArray]]; 

你为什么不通过getter访问它? 您可能正在使用另一个数组的实例

self.eventsArray[self eventsArray]

我build议使用fetchresultscontroller,我之前使用过数组,但FRC更好。

在您的addAudioEntityToArray方法中,您对addObject的调用会将新事件添加到数组的END中,而不是开始。

因此,您需要发送到insertRowsAtIndexPaths [NSIndexPath indexPathForRow:[MusikerViewController recordingsArray].count-1 inSection:0]是:像[NSIndexPath indexPathForRow:[MusikerViewController recordingsArray].count-1 inSection:0]