UiveView InserTrowsatIndexPathsエラー
-
13-11-2019 - |
質問
私はほとんどすべての検索結果を通して見てきましたが、私のエラーは消えませんでした。2つのセクションと1行(配列から)で初期化されたテーブルビューがあります。セクションヘッダービューのボタンがあります。ボタンをクリックすると、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:]、/sourcache/uikit_sim/uikit-1912.3/uitableView.1046
解決
[arr count]
の戻り値としてtableView:numberOfRowsInSection:
を使用しているため、beginUpdates…endUpdates
ブロックの最後に、TableViewはTableViewセクションのそれぞれに2行があることを期待していますが、insertRowsAtIndexPaths:
への呼び出しは行のみを示します。セクション0になっています。
セクションに応じて異なる値を返すようにtableView:numberOfRowsInSection:
を修正する必要があります。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return [arr count];
} else {
return 1;
}
}
.
あなたの表ビューと一緒に魚のようなものがたくさんあることに注意してください:あなたは2つのセクションを持っています、しかしあなたは各セクションに同じ正確な行0を示しています。そしてあなたのセクション内の挿入行動はかなり当然のことです。0行0と1行目に1行目に1行目が1行目に行0の代わりに行1に行を挿入する必要があります。
NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]];
[self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];
. 他のヒント
下のコードで試してみてください
NSIndexPath *indexpath_add = [NSIndexPath indexPathForRow:0 inSection:0];
[[self myTableView] beginUpdates];
[[self myTableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexpath_add] withRowAnimation:UITableViewRowAnimationRight];
[[self myTableView] endUpdates];
. 所属していません StackOverflow