ios – UITableView不更新

ios – UITableView不更新,第1张

概述我正在使用Core Data模型和UITableViewController表视图.我的模型似乎工作得很好,但是当我向模型添加实体时,我的表视图没有更新.我相信我的模型正在工作的原因是因为当我添加一个实体时,在运行时没有任何内容显示在视图中,但是如果我剪切任务然后启动它,那么我之前创建的实体突然显示在表视图中. 这是我的表视图控制器 – AudioTableViewController.h #im 我正在使用Core Data模型和UItableVIEwController表视图.我的模型似乎工作得很好,但是当我向模型添加实体时,我的表视图没有更新.我相信我的模型正在工作的原因是因为当我添加一个实体时,在运行时没有任何内容显示在视图中,但是如果我剪切任务然后启动它,那么我之前创建的实体突然显示在表视图中.

这是我的表视图控制器 – 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.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;}

如果有更多可能相关的方法,请告诉我,我会很乐意发布它们.

我还应该提一下,我可以在启动应用程序后向阵列添加单个实体,但之后会出现同样的问题.

解决方法 我认为您的问题是视图控制器的体系结构.试试这个:

1)在app delegate中设置所有核心数据堆栈.将关联的托管对象上下文作为属性传递给所有视图控制器.这就是Apple在许多示例代码示例中使用的模式.

2)对于每个表视图控制器,创建自己的NSFetchedResultsController,通常也作为属性.使用Apple样本作为指南.

3)永远不要像在代码中那样从其他视图控制器中获取任何数据.您不能也不应该从MusikerVIEwController获取AudiotableVIEwController的表数据.

拥有一个为查看控制器提供数据的中央数据对象是可能的,有时也是有用的.您不能将此数据对象作为视图控制器.这是对MVC设计模式的公然(并且在您的情况下是致命的)违反.您的错误很可能源于此(因为MusikerVIEwController不在屏幕上,甚至可能被释放).

4)确保在数据源方法中仅使用获取的结果控制器来填充表并传递媒体.

总结

以上是内存溢出为你收集整理的ios – UITableView不更新全部内容,希望文章能够帮你解决ios – UITableView不更新所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1114368.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存