0

セクションの最後の行を削除すると NSInternalInconsistencyException でアプリが終了するという UITableView に問題があります。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

私の UITableView には、MPMediaItemCollection (self.detailCollection) からの MPMediaItems が取り込まれます。最後のものが削除されたら、空白のセルに「結果が見つかりません」というラベルを表示したいと思います。

ここに私の cellForRowAtIndexPath があります:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...

if ([[self.detailCollection items] count] == 0) {
    [tableView numberOfRowsInSection:1];
    cell.textLabel.text = @"No results found";
    //return cell;
} else {

 MPMediaItem *song = (MPMediaItem *)[[self.detailCollection items] objectAtIndex:[indexPath row]];
 if (song) {

 cell.textLabel.text = [song valueForProperty:MPMediaItemPropertyTitle];
     MPMediaItemArtwork *art = [song valueForProperty:MPMediaItemPropertyArtwork];
     cell.imageView.image = [art imageWithSize:CGSizeMake(64, 64)];

 }
 }
   return cell;
 }

行を削除するための私のコードは次のとおりです。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
PlaylistData *pData = [PlaylistData getInstance];
NSMutableArray *tempArray = [[self.eventDictionary valueForKey:[NSString stringWithFormat:@"%@", pData.selectedEvent]] mutableCopy];
NSMutableArray *newArray = [[NSMutableArray alloc] init];
if (editingStyle == UITableViewCellEditingStyleDelete) {

    [tableView beginUpdates];
    // Delete the row from the data source
    [tempArray removeObjectAtIndex:indexPath.row];
    [self.eventDictionary setValue:tempArray forKey:[NSString stringWithFormat:@"%@", pData.selectedEvent]];
    [[NSUserDefaults standardUserDefaults] setValue:self.eventDictionary forKey:@"Playlist Items"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    if ([tempArray count] == 0) {
        [tableView numberOfRowsInSection:1];
    }
    for (int i=0; i<[tempArray count]; i++) {

        NSString *pID = [NSString stringWithFormat:@"%@", [tempArray objectAtIndex:i]];
        unsigned long long ullvalue = strtoull([pID UTF8String], NULL, 0);
        NSNumber *UniqueID = [NSNumber numberWithUnsignedLongLong:ullvalue];
        MPMediaQuery *cellQuery = [[MPMediaQuery alloc] init];
        [cellQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:UniqueID forProperty:MPMediaItemPropertyPersistentID]];

        for (MPMediaItem *item in [cellQuery items]) {
            [newArray addObject:item];
        }
        [cellQuery release];

    }
    if (![newArray count] == 0) {

    self.detailCollection = [[MPMediaItemCollection alloc] initWithItems:newArray];
    [tableView numberOfRowsInSection:[self.detailCollection count]];
    } else {
        [tableView numberOfRowsInSection:1];

        [tableView reloadData];
    }
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [tableView endUpdates];

}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}   
}

そして、ここに私のnumberOfRowsInSectionがあります:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Return the number of rows in the section.


if ([[self.detailCollection items] count] == 0 || [self.detailCollection items] == nil || [self.detailCollection items] == NULL) {

return 1;
}

return [[self.detailCollection items] count];
}

私の質問は: self.detailCollection が == 0 のときに「結果が見つかりません」セルを作成しないのはなぜですか?

4

3 に答える 3

1

次のような効果が欲しいと思います:

 [tableView beginUpdates];       

 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

 if ([newArray count] == 0) {

       [tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
 }

 [tableView endUpdates];

ただし、より簡単な解決策は、テーブル ビューにラベルを追加することです。「結果が見つかりません」を表示するために実際の UITableViewCell が必要な特定の理由がない限り。

UILabel *label = [[UILabel alloc] init];
CGRect frame = CGRectMake(0.0, 0.0, 320.0, 44.0);
label.frame = frame;
label.text = @"No results found";
[self.tableView addSubview:label];
于 2013-06-20T18:32:06.597 に答える
0

私がお勧めする解決策の 1 つは、新しいセルではなくテーブル フッター ビューを使用することです。基本的に、セル数が 0 の場合にのみ表示されるフッターをテーブルに追加します。

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section フッターを取得するメソッドをオーバーライドできます 。

オブジェクトを削除および追加する場合は、新しいカウントを確認してから、そこからフッターの表示を調整します。

于 2013-06-20T19:17:59.527 に答える
0

いくつかの場所で numberOfRowsInSection を呼び出しています。決して呼び出してはいけません。これは、実装するコールバックフックであり、システムが呼び出します。

これを行うための最もクリーンな解決策はself.tableView.tableFooterView、行 = 0 のときに設定することです

@interface UITableView : UIScrollView <NSCoding> {

...

@property(nonatomic,retain) UIView *tableFooterView; 
// accessory view below content. default is nil. not to be confused with section footer
于 2013-06-20T19:52:34.780 に答える