0

我的 UITableView 出现问题,删除该部分中的最后一行会导致应用程序终止并出现 NSInternalInconsistencyException:

*** 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

我推荐的一种解决方案是使用表格页脚视图而不是新单元格。基本上,在表格中添加一个仅在单元格计数为 0 时可见的页脚。

您可以重写该方法 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section 以获取页脚。

删除和添加对象时,检查新计数,然后从那里调整页脚的可见性。

于 2013-06-20T19:17:59.527 回答
0

您在几个地方调用 numberOfRowsInSection。您永远不应该调用它,它是您实现和系统调用的回调挂钩。

最干净的解决方案是self.tableView.tableFooterView在 rows = 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 回答