ios - UITableView 没有更新

标签 ios objective-c core-data uitableview

我正在使用核心数据模型和 UITableViewController TableView 。我的模型似乎工作得很好,但是当我向模型添加实体时,我的 TableView 没有更新。我相信我的模型正在运行的原因是因为当我添加一个实体时,在运行时 View 中没有任何显示,但是如果我削减任务然后重新启动它,我之前创建的实体突然出现在 TableView 中。

这是我的 TableView Controller - 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];
    }
}

...更多方法

这是我的银行类(class)的一部分,可能相关 银行.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

银行.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;

}

播放器.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

播放器.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;
}

如果还有其他可能相关的方法,请告诉我,我会很乐意发布它们。

我还应该提一下,我可以在启动应用程序后将单个实体添加到数组,但之后还是同样的问题。

最佳答案

可能是您的 - (void)addAudioEntityToArray:(AudioFile *)event {} 方法是从辅助线程(MainUi 线程除外)调用的,因此直接重新加载表将不起作用。

试试看

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

希望对您有所帮助!!

关于ios - UITableView 没有更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17940662/

相关文章:

ios - 使 com.apple.CoreData.ConcurrencyDebug 1 工作

ios - 哪些数据类型与 Core Data/SQL 一起使用以最大限度地减少转换

ios - 如何删除 Xcode 7 项目上的所有本地和远程 Git

ios - 带半径的中心坐标转换为 ne 和 sw 坐标

objective-c - 为什么复制指针以解决 ARC 下基于 block 的保留循环是有意义的?

objective-c - 当 UISearchController 处于事件状态并且选择了新选项卡时,UITableView 消失

ios - 如何按列值对 CoreData 中的记录进行分组?

ios - 构建 nspredicate 时出现 EXC_BAD_ACCESS

iOS:拍摄低质量图像

objective-c - (lldb) 以十六进制打印 unsigned long long