将行插入UITableView崩溃

时间:2012-12-20 04:59:06

标签: iphone objective-c ios uitableview insert

开发应用程序时,需要在后台加载一些数据,然后使用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).

有什么问题?请帮忙。

2 个答案:

答案 0 :(得分: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更新..这将解决

答案 1 :(得分:0)

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