ios - 无效的 TableView 更新

标签 ios objective-c uitableview

我有 1 个空白部分和其他充满对象的部分。我在所有行中都有一个按钮。当单击一行中的按钮时,该行应移动到空白部分。但是我收到此错误消息,指出无效的表格 View 更新。

*** 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];


}

最佳答案

尝试像这样的 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];
}

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

关于ios - 无效的 TableView 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17108645/

相关文章:

objective-c - 无符号字符数组到十六进制表示 NSString

iphone - UITableView 使用 PhoneGap 的外观和感觉

ios - 如何将一组 HKQuantitySample(心率)保存到锻炼中

objective-c - 非 ARC block ,内存泄漏问题

ios - Objc <-> Swift 泛型函数的兼容性

iphone - dealloc,使用 release 或设置为 nil 属性?

ios - 在 Swift 1.2 中将 self 作为参数传递给 init 方法

ios - 按下 UINavigationController 的后退栏按钮时执行操作

ios - 解析XML问题

ios - 滚动表格 View 时,标签的角半径变得扭曲