2

我正在开发一个应用程序,需要在后台加载一些数据,然后使用 UITableView 显示数据。这是一些代码,
在后台加载数据:

- (void)loadRelatedItems 
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    for (NSString *mediaType in allMediaTypes) 
    {
        [self performSelector:@selector(loadRelatedItems:) withObject:mediaType];
    }

    NSString *notificationName = [CommonFunction allRelatedItemsLoadedNotificationName];
    [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:self userInfo:nil];

    [pool release];
}

- (void)loadRelatedItems:(NSString *)mediaType 
{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

   for (NSString *keyword in _keywords) 
   {
      NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@&mediaType=%@&keyword=%@", API, mediaType, keyword]];
      NSMutableArray *items = [CommonFunctions arrayFromURL:URL];

      if ([items count] == 0) continue;

      NSString *notificationName = [CommonFunction partialRelatedItemsLoadedNotificationName];
      NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:items, @"items", mediaType, @"mediaType", nil];
      [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:self userInfo:dic];   
   }

   [pool release];
}

在 UITableView 中显示数据:

- (void)didFinishLoadPartialRelatedItems:(id)sender 
{
  NSDictionary *dic = [sender userInfo];
  NSString *mediaTypeString = [dic objectForKey:@"mediaType"];
  NSMutableArray *items = [dic objectForKey:@"items"];


  dispatch_async(dispatch_get_main_queue(), ^{

    if ([_relatedItems count] == 0) 
    {
        [_relatedItems setObject:items forKey:mediaTypeString];
        [_tableView reloadData];
    } 
    else 
    {
        NSMutableArray *mediaTypeItems = [_relatedItems objectForKey:mediaTypeString];

        if (mediaTypeItems) 
        { 
           // section exist
           NSInteger section =[[[_relatedItems allKeys] sortedArrayUsingSelector:@selector(mediaTypeCompare:)] indexOfObject:mediaTypeString];
           NSMutableArray *indexPaths = [NSMutableArray array];

           for (NSMutableDictionary *item in items) 
           {
               [mediaTypeItems addObject:item];
               NSInteger newRow = [mediaTypeItems indexOfObject:item];
               NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:newRow inSection:section];
               [indexPaths addObject:newIndexPath];
           }

           [_tableView beginUpdates];
           [_tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
           [_tableView endUpdates]; 
        }
        else 
        { 
            // new section

            [_relatedItems setObject:items forKey:mediaTypeString];
            NSInteger section =[[[_relatedItems allKeys] sortedArrayUsingSelector:@selector(mediaTypeCompare:)] indexOfObject:mediaTypeString];
            NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];
            [_tableView beginUpdates];
            [_tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
            [_tableView endUpdates];

        }

    }
});
}

#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if ([_relatedItems count] == 0) {
    return 1;
} else {

    return [_relatedItems count];
}
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

NSArray *allTitles = [[_relatedItems allKeys] sortedArrayUsingSelector:@selector(mediaTypeCompare:)];
NSString *title = [allTitles objectAtIndex:section];
NSDictionary *allMediaTypeDisplayNames = [CommonFunction allMediaTypeDisplayNames];

return [allMediaTypeDisplayNames objectForKey:title];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([_relatedItems count] == 0) {
    return 0;
}
NSArray *allTitles = [[_relatedItems allKeys] sortedArrayUsingSelector:@selector(mediaTypeCompare:)];

NSString *title = [allTitles objectAtIndex:section];
NSInteger rowsCount = [[_relatedItems objectForKey:title] count];

return rowsCount;
}

我很困惑它有时可以正常工作,但有时它会因消息而崩溃:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1030
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (0) must be equal to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).

有什么问题?请帮忙。

4

2 回答 2

2

请确保更新后,您的节数应等于更新前的节数。

根据您的代码:

部分的数量定义如下:

if ([_relatedItems count] == 0) {
    return 1;
} else {

    return [_relatedItems count];
}

在这种情况下,您正在创建新部分,对吗?

else { // new section

            [_relatedItems setObject:items forKey:mediaTypeString];
            NSInteger section =[[[_relatedItems allKeys] sortedArrayUsingSelector:@selector(mediaTypeCompare:)] indexOfObject:mediaTypeString];
            NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];
            [_tableView beginUpdates];
            [_tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
            [_tableView endUpdates];

        }

如果您正在创建新部分,那么您[_relatedItems count]正在增加。因此,请确保在插入后您的计数也应该相同。

正确的 ?

尝试这个 :

if ([_relatedItems count] == 0) {
        return 1;
    } else {
        if([_relatedItems count]>previousCount)
            return [_relatedItems count];
        return previousCount;
    }

当您对 then 进行任何更新时,[_relatedItems];也更新您的 previousCount .. 这将得到解决

于 2012-12-20T05:12:07.910 回答
0

我认为您的问题在于数据源中的行数方法只需做一件事,在您的 .h 文件中定义 int noOfRows 。使用 noOfRows 分配您的表视图数组计数。 noOfRows=[yourtableArray count]; 然后从表视图 numberOfRowsInSection 方法返回 noOfRows。如果在表中插入行,则向 noOfRows 添加 1。制作 noOfRows——当你从表中删除行时你不会得到这个异常。根据你的要求更新你的数组。

于 2012-12-20T05:11:27.323 回答