【发布时间】:2016-10-30 03:58:27
【问题描述】:
在我的 iOS 应用程序中,我有一个表格视图,它有 2 个部分/2 个数组。我正在尝试将对象从一个数组/部分移动到另一个数组/部分。现在第 0 节称为 followArray,第 1 节称为 dataArray,dataArray 存储构成每个单元格的所有数据。因此,当用户单击我设置的名为 Follow 的按钮时,它应该将该单元格从 dataArray/Section 1 中删除并将其插入到 followArray/Section 0 中。但是当我尝试这样做时,我得到一个错误这么说的。
错误信息
*** -[UITableView _endCellAnimationsWithContext:] 中的断言失败,/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3599.6/UITableView.m:1396
*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试将第 0 行插入第 0 节,但更新后第 0 节中只有 0 行”
代码:
表格视图
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configuring the cell
Data * dataObject;
if (!isFiltered) {
if (indexPath.section == 0) {
dataObject = [followedArray objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1) {
dataObject = [dataArray objectAtIndex:indexPath.row];
}
}
else {
dataObject = [filteredArray objectAtIndex:indexPath.row];
}
// Loading Button
cell.followButton.tag = indexPath.row;
[cell.followButton addTarget:self action:@selector(followButtonClick:) forControlEvents:UIControlEventTouchUpInside];
cell.followButton.hidden = NO;
return cell;
}
部分标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return @"Followed Data";
}
else {
return @"All Data";
}
}
行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (!isFiltered) {
if (section == 0) {
return [followedArray count];
}
else if (section == 1) {
return [dataArray count];
}
}
return [filteredArray count];
}
------------------------------------------ ---------
-- 这是动作发生的地方--
------------------------------------------ ---------
关注按钮
-(void)followButtonClick:(UIButton *)sender {
// Adding row to tag
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition];
// Creating an action per tag
if (indexPath != nil)
{
NSLog(@"Current Row = %@", indexPath);
// ----- ERROR HERE -----
[self.myTableView beginUpdates];
// ----- Inserting Cell to Section 0 ----- *NOT WORKING*
[followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:indexPath.row];
NSInteger rowToAdd = indexPath.row;
[self.myTableView insertRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToAdd inSection:0], nil] withRowAnimation:YES];
// ----- Removing Cell from Section 1 ----- *WORKING*
[dataArray removeObjectAtIndex:indexPath.row];
NSInteger rowToRemove = indexPath.row;
[self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];
[self.myTableView endUpdates];
}
}
【问题讨论】:
-
删除 beginUpdates 和 endUpdates。立即尝试!
-
@Manigandasaravanan 同样的错误
-
// 添加行到标签 CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView]; NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition];为什么要为 IndexPath 这样做??
-
@Dhanesh 这就是我在每个单元格中的按钮给我一个不同的标签,为什么,它会导致冲突吗?
-
NSLog(@"当前行 = %@", indexPath);你在这里得到什么?
标签: ios objective-c uitableview nsmutablearray