gpt4 book ai didi

ios - 就地 UITableView 编辑 : Adding a row crash

转载 作者:行者123 更新时间:2023-12-02 12:19:48 25 4
gpt4 key购买 nike

我的表格 View 使用就地编辑和添加。当按下编辑按钮时会添加一个新行,并且可以编辑和提交该新行。将新行提交到 TableView 中时,将添加新行并将空白单元格向下移动,随后可以对其进行编辑并再次添加另一行。但是,当尝试在同一编辑 session 期间添加另一行时,我收到错误:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:]
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'attempt to insert row 2 into section 0, but there are only 2 rows in section 0 after the update'

之前,我在仅添加一行时遇到了此错误,但在遵循就地编辑教程后,我认为我已经解决了这个问题。以下是一些相关方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

int count = [items count];
if(self.editing)count++;
return count;

}



- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];

[keyphraseTextField setEnabled:YES];

NSArray *paths = [NSArray arrayWithObject:
[NSIndexPath indexPathForRow:[items count] inSection:0]];
if (editing)
{
[[self tableView] insertRowsAtIndexPaths:paths
withRowAnimation:UITableViewRowAnimationTop];
}
else {
[[self tableView] deleteRowsAtIndexPaths:paths
withRowAnimation:UITableViewRowAnimationTop];
}
}



- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {

[self deleteItemAtIndexPath:indexPath];

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

else if (editingStyle == UITableViewCellEditingStyleInsert) {

NSIndexPath *thePath = [NSIndexPath indexPathForRow:[items count] inSection:0];

DBAccess *dbaccess = [[DBAccess alloc] init];

NSInteger keyphraseCount = [dbaccess getDomainKeyphraseCount:domain_id];
NSInteger keyphraseCountLimit = [dbaccess getDomainKeyphraseCountLimit:domain_id];

[dbaccess closeDatabase];

[dbaccess release];

if(![keyphraseTextField.text isEqualToString:@""] && keyphraseCount<keyphraseCountLimit){

[self insertItemAtIndexPath:thePath];

[self readItems];

[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:thePath] withRowAnimation:UITableViewRowAnimationLeft];

[self makeNSURLConnection];

}else{

if([keyphraseTextField.text isEqualToString:@""]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Please insert Keyphrase." delegate:self cancelButtonTitle:@"Not Now." otherButtonTitles:@"Upgrade!", nil];
[alert setTag:10];
[alert show];
[alert release];
}
if(keyphraseCount>=keyphraseCountLimit){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Kindly upgrade your subscription to add more keyphrases!" delegate:self cancelButtonTitle:@"Not Now." otherButtonTitles:@"Upgrade!", nil];
[alert setTag:10];
[alert show];
[alert release];
}

}

}

}



- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;

if (self.editing && indexPath.row == ([items count])) {

return UITableViewCellEditingStyleInsert;

} else {

return UITableViewCellEditingStyleDelete;

}

return UITableViewCellEditingStyleNone;

}

如何解决此问题,以便用户能够在单个编辑“ session ”中逐行添加?

最佳答案

您应该将插入/删除/选择行放在 [self.tableview beginUpdates];[self.tableview endUpdates];

之间

关于ios - 就地 UITableView 编辑 : Adding a row crash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12915674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com