0

拡張可能なテーブルビューセルに関する問題に直面しています。テーブルビューセルを展開しようとするとクラッシュします。アプリがクラッシュする場所がわかりません。解決策を教えてください。私を助けてください。

私のコード:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   if (appDelegate.array_proj == (id)[NSNull null])
        return 0;
    else
        return [appDelegate.array_proj count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if ([expandedSections containsIndex:section])
    {
        if ([appDelegate.array_task count]==0)
        {
            return 0;
        }
        else
        {
            NSLog(@"array task count: %d",[appDelegate.array_task count]);
            return [appDelegate.array_task count];
        }
    }
    return 1;
}

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

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor colorWithRed:53.0/255 green:53.0/255 blue:53.0/255 alpha:1.0];

    UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:CGRectZero];
    cell.backgroundView = backgroundView;
    backgroundView.image = [UIImage imageNamed:@"prjctcell_bg.png"];

    if (!indexPath.row)
    {
        objePro = [appDelegate.array_proj objectAtIndex:indexPath.section];

        cell.textLabel.text = objePro.projctname;
        appDelegate.intForPid=objePro.pojctid;

        if ([expandedSections containsIndex:indexPath.section])
        {
            cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor blackColor] type:DTCustomColoredAccessoryTypeUp];
        }
        else
        {
            cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor blackColor] type:DTCustomColoredAccessoryTypeDown];
        }
    }
    else
    {
        if (appDelegate.array_task != (id)[NSNull null])
        {
            objePro = [appDelegate.array_proj objectAtIndex:appDelegate.storeAppDelegateIndex];
            objeTask = [appDelegate.array_task objectAtIndex:indexPath.section];

            cell.textLabel.text = objeTask.taskname;
            cell.backgroundView = nil;
            cell.accessoryView = nil;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
{
    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    objePro = [appDelegate.array_proj objectAtIndex:indexPath.section];
    appDelegate.intForPid=objePro.pojctid;

   [appDelegate selectTask:appDelegate.intForPid];

    if (!indexPath.row)
    {
        [tblView beginUpdates];

        //only first row toggles expand/collapse
        [tblView deselectRowAtIndexPath:indexPath animated:YES];

        NSInteger sections = indexPath.section;
        BOOL currentlyExpanded = [expandedSections containsIndex:sections];
        NSInteger rows;

        NSMutableArray *tmpArray = [NSMutableArray array];
        if (currentlyExpanded)
        {
            rows = [self tableView:tblView numberOfRowsInSection:sections];
            [expandedSections removeIndex:sections];
        }
        else
        {
            [expandedSections addIndex:sections];
            rows = [self tableView:tblView numberOfRowsInSection:sections];
        }
        for (int i=1; i<rows; i++)
        {
            NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i inSection:sections];
            [tmpArray addObject:tmpIndexPath];
        }
        UITableViewCell *cell = [tblView cellForRowAtIndexPath:indexPath];

        if (currentlyExpanded)
        {

            [tblView deleteRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];
            cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor blackColor] type:DTCustomColoredAccessoryTypeDown];
        }
        else
        {
            [tblView insertRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];
            cell.accessoryView =  [DTCustomColoredAccessory accessoryWithColor:[UIColor blackColor] type:DTCustomColoredAccessoryTypeUp];
        }
        [tblView endUpdates];
    }
}

エラーが発生します: -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1070 2013-03-20 19:14:00.102 Daily Achiever[2886:c07] でのアサーションの失敗*原因によるアプリの終了キャッチされない例外 'NSInternalInconsistencyException'、理由: '無効な更新: セクション 1 の行数が無効です。更新後の既存のセクションに含まれる行数 (3) は、更新前にそのセクションに含まれる行数と等しくなければなりません(1)、そのセクションから挿入または削除された行の数 (挿入 0、削除 0) のプラスまたはマイナス、およびそのセクションに移動された行の数 (移動された 0、移動された 0) のプラスまたはマイナス。

4

2 に答える 2

0

セクションを追加するときは、このコードを使用します

NSArray *insertIndexPaths = [NSArray arrayWithObjects:
                             [NSIndexPath indexPathForRow:2 inSection:0],
                             [NSIndexPath indexPathForRow:3 inSection:0],
                             nil];

行の挿入用

[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];

2 つのセクションを追加する場合

 [tableView insertSections:[NSIndexSet indexSetWithIndex:indexPath.section + 1] withRowAnimation:UITableViewRowAnimationAutomatic];
   [tableView insertSections:[NSIndexSet indexSetWithIndex:indexPath.section + 2] withRowAnimation:UITableViewRowAnimationAutomatic];

同じようにセクションを削除

[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:YES];
    [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section - 1] withRowAnimation:YES];

行の削除

 [tableView deleteRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
于 2013-03-20T15:38:14.140 に答える
0

Your problem is in these lines of code:

 [tblView deleteRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];

and

 [tblView insertRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];

The crash that you posted was due to the first one. In these lines, you are trying to insert/delete rows from the table, but your - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method does not change correspondingly.

It looks like you want to add/remove items from your appDelegate.array_task array whenever you are adding/removing cells from the table (as this is the array from which you are determining the row count).

As a side note, the line:

 if ([appDelegate.array_task count]==0) { 
    return 0;
 }

is not doing anything, because you have else return [appDelegate.array_task count] which means that 0 will be returned if [appDelegate.array_task count]==0 anyway.

于 2013-03-20T13:57:24.060 に答える