0

我有 1 个空部分和其他部分充满了对象。我在所有行中都有一个按钮。当单击一行中的按钮时,该行应该移动到空部分。但是我收到这个错误,说无效的 tableview 更新。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid table view update.  The application has requested an update to the table view that is inconsistent with the state provided by the data source.'

我尝试了以下代码。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ([[arrayForBool objectAtIndex:section] boolValue]) {
    if(section == 0){
        return favouritesArray.count;
    }else{
    NSString* key = [sectionTitleArray objectAtIndex:section - 1];
    NSArray *aArray = [sectionContentDict valueForKey:key];
    return aArray.count;
    }
}
return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"UCPMenuCellIdentifier";
UCPCategoryListCell *cell = (UCPCategoryListCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[[UCPCategoryListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
}


if(indexPath.section == 0){
    cell.textLabel.text = [favouritesArray objectAtIndex:indexPath.row ];
}
else{
NSString* key = [sectionContentDict.allKeys objectAtIndex:indexPath.section - 1];
BOOL manyCells  = [[arrayForBool objectAtIndex:indexPath.section] boolValue];

if (!manyCells) {
    cell.textLabel.text = @"click to enlarge";
}
else{
    cell.textLabel.text = [[sectionContentDict objectForKey:key] objectAtIndex:indexPath.row];
}
}

UIButton *deletebtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
deletebtn.frame=CGRectMake(220, 10, 20, 20);
[deletebtn addTarget:self action:@selector(moveRowToFavorites:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deletebtn];

return cell;
}


-(void)moveRowToFavorites:(id)sender{
 UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
    [self.aTableView beginUpdates];
    [favouritesArray addObject:cell.textLabel.text];
    NSIndexPath *newindexPath = [NSIndexPath indexPathForRow:[favouritesArray count] inSection:0];
     NSIndexPath *oldindexPath = [self.aTableView indexPathForCell:cell];
    [self.aTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:oldindexPath] withRowAnimation:UITableViewRowAnimationFade];
    [self.aTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newindexPath] withRowAnimation:UITableViewRowAnimationRight];
    [self.aTableView endUpdates];


}
4

3 回答 3

1

在 moveRowToFavorites 方法中更改数组(favouritesArray 和 sectionContentDict)。您已添加到 favouritesArray。从其他数组中删除相同的对象。然后调用表reloadData。尝试这个。它将添加到收藏夹部分。

-(void)moveRowToFavorites:(id)sender{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;

    [favouritesArray addObject:cell.textLabel.text];
    //here you need to delete same object from [sectionContentDict objectForKey:key] 
    [self.aTableView reloadData];
}

不要做任何类似insertRowsAtIndexPathsdeleteRowsAtIndexPaths

于 2013-06-14T12:47:52.027 回答
1

尝试这样的 moveRowToFavorites: 方法:

- (void) moveRowToFavorites: (id) sender
{
    UIButton* button = (UIButton*) sender;
    UITableViewCell* cell = (UITableViewCell*) button.superview.superview;
    [self.aTableView beginUpdates];
    [favouritesArray addObject: cell.textLabel.text];

    NSIndexPath* newindexPath = [NSIndexPath indexPathForRow: [favouritesArray count] - 1
                                                   inSection: 0];
    [self.aTableView insertRowsAtIndexPaths: [NSArray arrayWithObject: newindexPath]
                          withRowAnimation: UITableViewRowAnimationRight];

    NSIndexPath* oldindexPath = [self.aTableView indexPathForCell: cell];
    [self.aTableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: oldindexPath]
                          withRowAnimation: UITableViewRowAnimationFade];

    NSInteger oldSection = oldindexPath.section;
    NSInteger oldRow = oldindexPath.row;
    NSString* key = [sectionTitleArray objectAtIndex: oldSection - 1];
    NSMutableArray* anArray = [NSMutableArray arrayWithArray: [sectionContentDict valueForKey: key]];
    [anArray removeObjectAtIndex: oldRow];
    [sectionContentDict setValue: anArray forKey: key];

    [self.aTableView endUpdates];
}

我用这段代码创建了项目。它工作正常。如果你需要我可以给。希望我的代码对你有所帮助。

于 2013-06-14T18:42:00.477 回答
0

-(void)moveRowToFavorites:(id)sender刚刚将对象添加到 之后favouritesArray,您必须从sectionContentDict. 并且您不必在开始更新后使用 [table reloadData]。

于 2013-06-14T12:50:31.960 回答