![](/img/trans.png)
[英]When is tableView:numberOfRowsInSection: called in UITableView?
[英]tableView:numberOfRowsInSection: called twice in a row
我收到一個錯誤:
由於未捕獲的異常“ NSInternalInconsistencyException”而終止應用程序,原因:“無效的更新:第0節中的行數無效。
因為我要將對象添加到數組的一個實例中,但是隨后從應該是同一數組的另一個實例中重新加載表視圖。 如何創建數組的單個實例,然后在各個類之間傳遞它? 我可以在Java中輕松完成此操作,但是在Objective-C中,您無法創建靜態變量,因此我不確定如何執行此操作。
編輯:更多代碼。
這是另一個類的方法,為了保存文件而調用該方法。 我正在使用Core Data,因此首先將文件添加到上下文(模型),然后添加到數組,然后保存上下文。 此方法在名為“玩家”的類中
-(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的重要部分-跟蹤recordsArray和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;
}
如果您的數據模型發生更改,則需要重新加載tableView或插入/刪除行,否則tableview會對您不滿意。 調用tableView:numberOfRowsInSection的次數無關緊要,您的數據模型必須先重新加載或更新tableview才能返回不同的數字。
根據您的記錄,我的猜測是您的下載陣列正在改變數量。 它經過NSLog,然后在_endCellAnimationsWithContext中死亡。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.