2012-02-28 15 views
1

ほとんどすべての検索結果を調べましたが、エラーは消えません。私は2つのセクションと1行のそれぞれ(配列から)で初期化されたテーブルビューを持っています。セクションヘッダービュー用のボタンがあります。ボタンをクリックすると、最初のセクションに行を追加します。ここでは、コードです:UITableView insertRowsAtIndexPathsエラー

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

HERESにエラー:

* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046

答えて

2

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

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

魚多くのものは、あなたのtableViewで起こっていることがあります
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (section == 0) { 
     return [arr count]; 
    } else { 
     return 1; 
    } 
} 

注:2つのセクションがありますが、あなたは、各セクションにまったく同じ行0を示しています。あなたのセクション内の挿入動作は非常に奇妙です:あなたはswitchのcase 0に対応する1行だけを表示し始めます。その後、行0に行を挿入したことを示します。その後、ケースを表示します行0時0行とあなたが本当に代わりに、行0の行1で行を挿入する必要があります行1の場合1行目:

NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]]; 
[self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom]; 
0

NSIndexPath *indexpath_add = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [[self myTableView] beginUpdates]; 
    [[self myTableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexpath_add] withRowAnimation:UITableViewRowAnimationRight]; 
    [[self myTableView] endUpdates]; 
+0

だけでなく、下記のコードにしてみてくださいアサーションエラーが発生しますが、テーブルビューに行を追加しようとすることさえありません。これは、OPがやりたいことです。 – yuji

関連する問題