我有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];
}
答案 0 :(得分:1)
在moveRowToFavorites方法中更改数组(favouritesArray和sectionContentDict)。你已经在favouritesArray中添加了。从其他数组中删除相同的对象。然后调用table 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];
}
不要执行insertRowsAtIndexPaths
或deleteRowsAtIndexPaths
答案 1 :(得分: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];
}
我使用此代码创建了项目。它工作正常。如果你需要我可以给。 希望我的代码可以帮助你。
答案 2 :(得分:0)
在向-(void)moveRowToFavorites:(id)sender
添加对象后的favouritesArray
中,您必须从sectionContentDict
中的数组中删除对象。而且你不必在开始结束更新后使用[table reloadData]。