删除TableView中的最后一行并显示"无结果"新细胞中的标签

时间:2013-06-20 17:25:51

标签: ios uitableview

我的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时,为什么不创建“找不到结果”单元?

3 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

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

您可以覆盖该方法 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section 得到一个页脚。

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

答案 2 :(得分:0)

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

执行此操作的最干净的解决方案是在行= 0

时设置self.tableView.tableFooterView
@interface UITableView : UIScrollView <NSCoding> {

...

@property(nonatomic,retain) UIView *tableFooterView; 
// accessory view below content. default is nil. not to be confused with section footer