1

ほぼすべての検索結果に目を通しましたが、エラーが解消されません。2 つのセクションとそれぞれ 1 つの行 (配列から) で初期化されたテーブル ビューがあります。セクション ヘッダー ビューのボタンがあります。ボタンをクリックすると、最初のセクションに行を追加したいと思います。コードは次のとおりです。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    UITableViewCell *cell = (UITableViewCell*)[self.myTableView dequeueReusableCellWithIdentifier:@"DetailCell"];
    cell.textLabel.text=[arr objectAtIndex:indexPath.row];
    cell.backgroundColor=[UIColor clearColor];
    return cell; 
} 

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            btnReleases=[UIButton buttonWithType:UIButtonTypeCustom];
            [btnReleases setFrame:CGRectMake(10, 0, 30, 39)];
            [btnReleases setImage:[UIImage imageNamed:@"buttonr.png"] forState:UIControlStateNormal];
            [btnReleases addTarget:self action:@selector(loadReleases) forControlEvents:UIControlEventTouchUpInside];
            return btnReleases;
            break;
        case 1:
            btnLinks=[UIButton buttonWithType:UIButtonTypeCustom];
            [btnLinks setFrame:CGRectMake(10, 0, 30, 39)];
            [btnLinks setImage:[UIImage imageNamed:@"buttonl.png"] forState:UIControlStateNormal];
            [btnLinks addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
            return btnLinks;
            break;
        default:
            break;
    }

}
-(void)loadReleases
{

    [self.myTableView beginUpdates];
        [arr addObject:@"WTF"];
    NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; NSLog(@"%@",insert0);
    [self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];
    [self.myTableView endUpdates];
}

エラーは次のとおりです。

* -[UITableView _endCellAnimationsWithContext:]、/SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046 でのアサーションの失敗

4

2 に答える 2

2

[arr count]の戻り値としてを使用しているためtableView:numberOfRowsInSection:、ブロックの最後で、beginUpdates…endUpdatestableViewは各tableViewセクションに2つの行があることを期待していますが、への呼び出しinsertRowsAtIndexPaths:は、行がセクション0にあることを示しているだけです。

tableView:numberOfRowsInSection:セクションに応じて異なる値を返すように修正する必要があります。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return [arr count];
    } else {
        return 1;
    }
}

tableViewでは多くの厄介なことが起こっていることに注意してください。2つのセクションがありますが、各セクションにまったく同じ行0が表示されています。また、セクション内の挿入動作はかなり不安定です。最初は、のケース0に対応する1つの行だけswitchを表示し、次に行0に行を挿入することを示し、その後、ケース0を表示します。行0の行と行1のケース1行。したがって、実際には行0ではなく行1に行を挿入する必要があります。

NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]]; 
[self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];
于 2012-02-28T09:42:48.453 に答える
0

以下のコードで試してください

NSIndexPath *indexpath_add = [NSIndexPath indexPathForRow:0 inSection:0];
     [[self myTableView] beginUpdates];
     [[self myTableView]  deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexpath_add] withRowAnimation:UITableViewRowAnimationRight];
     [[self myTableView] endUpdates];
于 2012-02-28T09:45:09.943 に答える