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;
    }

あなたが更新を行っているときはいつでも、[_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 に答える