关键词:
【中文标题】将节标题添加到uitableview后删除行的问题【英文标题】:Issue with deleting rows after adding section header to uitableview 【发布时间】:2013-10-17 13:19:45 【问题描述】:在将节标题添加到我的应用程序中的一个表之前,我能够使用 commitEditingStyle 函数删除行而没有任何问题。我决定实现部分标题,以便用户更轻松地查看按日期添加到表中的项目。此功能工作正常。在实现节标题后,我在删除行时遇到了问题,但感谢 *** 上好心人的帮助,问题得到了部分解决。经过一些测试,我意识到如果这些行在同一部分中,并且我尝试从该部分的第一行开始依次删除多行,则第一行删除得很好,但尝试删除第二行会导致应用程序崩溃。如果我按顺序删除第一行以外的所有行,然后最后删除第一行,它工作正常。 Xcode 没有在调试日志中指出它崩溃的原因。
下面是 cellForRowAtIndexPath 函数的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
AgendaCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[AgendaCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
NSString *strDate = [dateArray objectAtIndex:indexPath.section];
NSMutableArray *dateSection = [tempDict objectForKey:strDate];
NSManagedObject *object = [dateSection objectAtIndex:indexPath.row];
cell.sessionNameLabel.text = [object valueForKey:@"sessionname"];
cell.sessionNameLabel.textColor = [UIColor blueColor];
cell.sessionDateLabel.text = [object valueForKey:@"sessiondate"];
cell.sessionDateLabel.textColor = [UIColor brownColor];
cell.sessionTimeLabel.text = [object valueForKey:@"sessiontime"];
cell.sessionTimeLabel.textColor = [UIColor brownColor];
return cell;
这是我的表格刷新功能的代码:
- (void) refreshTable
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sessnotes" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"agenda == 'Yes'"]];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sessiondate" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
[self.refreshControl endRefreshing];
self.objects = results;
if (results.count == 0)
NSString *message = @"You have not added any sessions to your planner.";
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Notification"
message:message
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil,nil];
[alertView show];
else if (results.count > 0)
tempDict = nil;
tempDict = [[NSMutableDictionary alloc] init];
NSString *strPrevDate= [[results objectAtIndex:0] valueForKey:@"sessiondate"];
NSLog(@"strPrevDate value is: %@", strPrevDate);
NSString *strCurrDate = nil;
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
//Add the Similar Date data in An Array then add this array to Dictionary
//With date name as a Key. It helps to easily create section in table.
for(int i=0; i< [results count]; i++)
strCurrDate = [[results objectAtIndex:i] valueForKey:@"sessiondate"];
if ([strCurrDate isEqualToString:strPrevDate])
[tempArray addObject:[results objectAtIndex:i]];
else
[tempDict setValue:[tempArray copy] forKey:strPrevDate];
strPrevDate = strCurrDate;
[tempArray removeAllObjects];
[tempArray addObject:[results objectAtIndex:i]];
//Set the last date array in dictionary
[tempDict setValue:[tempArray copy] forKey:strPrevDate];
NSArray *tArray = [tempDict allKeys];
//Sort the array in ascending order
dateArray = [tArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
[self.tableView reloadData];
这里是 commitEditingStyle 函数的代码:
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
if (editingStyle == UITableViewCellEditingStyleDelete)
//add code here for when you hit delete
NSManagedObject *object = [self.objects objectAtIndex:indexPath.row];
NSManagedObjectContext *context = [self managedObjectContext];
[context deleteObject:[context objectWithID:[object objectID]]];
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error])
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
NSMutableArray *array = [self.objects mutableCopy];
[array removeObjectAtIndex:indexPath.row];
self.objects = array;
[tableView reloadData];
【问题讨论】:
【参考方案1】:前面有几件事。每次您想重新加载 tableview 时,我都不会发出 fetch 请求。你应该看看 NSFetchedResultsController。它会自动将数据绑定到您的 tableview 并根据来自相同 NSManagedObjectContexts 的更新或来自其他上下文的更新消息为您进行刷新,并为您批处理它们。
回答你原来的问题。我会尝试先从数组中删除对象,然后删除 NSManagedObject,然后你可以使用一些 tableview 技巧:
NSManagedObject *managedObject = self.array[indexPath.row];
[self.array removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationAutomatic];
[context deleteObject:managedObject];
我可能是错的,但有可能你没有完成一个错误。希望对您有所帮助。
【讨论】:
尝试将节页脚添加到 UICollectionView 时崩溃
】尝试将节页脚添加到UICollectionView时崩溃【英文标题】:CrashwhentryingtoaddsectionfootertoUICollectionView【发布时间】:2013-01-1902:13:03【问题描述】:我正在尝试向UICollectionView添加节页脚,但以我不理解的方式失败。我正在这样设置主... 查看详情
获取 UITableView 中最近添加的行的 indexPath
】获取UITableView中最近添加的行的indexPath【英文标题】:GetindexPathofrecentrowaddinUITableView【发布时间】:2013-07-0509:33:05【问题描述】:我有一个问题:我想获取最近添加到UITableView上的单元格的indexPath。这是我的代码:-(UITableViewCell... 查看详情
UITableView 中同时插入/删除行的问题
】UITableView中同时插入/删除行的问题【英文标题】:Problemswithsimultaneousinsertion/deletionofrowsinUITableView【发布时间】:2011-11-2216:27:13【问题描述】:这几天我一直在努力解决这个问题。我需要在beginUpdates/endUpdates块内的UITableView中同... 查看详情
添加标题视图后,滚动到 UITableView 底部导致 NSException
】添加标题视图后,滚动到UITableView底部导致NSException【英文标题】:Afteraddingheaderview,scrolltobottomofUITableViewcauseNSException【发布时间】:2017-02-0822:16:59【问题描述】:我有一个方法可以将我的UITableView滚动到底部:functableViewScrollToB... 查看详情
动态添加和删除 UITableViewCells 到 UITableView
】动态添加和删除UITableViewCells到UITableView【英文标题】:DynamicallyaddandremoveUITableViewCellstoUITableView【发布时间】:2010-12-1001:37:50【问题描述】:我正在构建一个应用程序,用户可以在其中提供他/她拥有的不同用户名。愿景是,用... 查看详情
UITableView commitEditingStyle 不删除单元格
】UITableViewcommitEditingStyle不删除单元格【英文标题】:UITableViewcommitEditingStylewithoutdeletingcell【发布时间】:2012-07-2609:02:30【问题描述】:我对删除UITableView中的行的概念有一些小问题。用户删除一行后我不想触发:[self.tableViewdelet... 查看详情
添加/删除 UITableView 选择到数组,复选标记在滚动时消失 - Swift
】添加/删除UITableView选择到数组,复选标记在滚动时消失-Swift【英文标题】:Add/RemoveUITableViewSelectiontoArray,CheckmarksDisappearuponscroll-Swift【发布时间】:2014-11-2215:59:10【问题描述】:问题1:滚动时复选标记不断消失。问题2:需要帮... 查看详情
用于在 Swift 中删除 UITableView 中的行的编辑按钮不起作用
】用于在Swift中删除UITableView中的行的编辑按钮不起作用【英文标题】:EditbuttonfordeletingrowsinUITableViewinSwiftisnotworking【发布时间】:2015-04-1520:02:37【问题描述】:我有一个带有自定义单元格类和导航控制器的表格。我向导航控制... 查看详情
如何启用移动但禁用 UITableView 中某些行的删除
】如何启用移动但禁用UITableView中某些行的删除【英文标题】:HowtoenablemovebutdisabledeleteforsomerowinUITableView【发布时间】:2015-03-1203:06:02【问题描述】:我的UITableView中的一些行需要在编辑模式下删除和移动,但有些只是移动。在... 查看详情
UITableView 以级联动画的方式进行行的删除和插入
】UITableView以级联动画的方式进行行的删除和插入【英文标题】:UITableViewperformingrowdeletionandinsertioninacascadinganimationmanner【发布时间】:2017-04-2016:58:21【问题描述】:我有兴趣使用动画tableView.deleteRows(at:[singleIndexPath],with:.automatic)... 查看详情
将 uitableview 添加到 uiview 后 didselectrowatindexpath 未调用 ios
】将uitableview添加到uiview后didselectrowatindexpath未调用ios【英文标题】:afteraddinguitableviewtouiviewdidselectrowatindexpathnotcalledios【发布时间】:2015-06-2510:11:34【问题描述】:我创建了一个自定义tableviewdisplayCustomUnitTableView和一个自定义UIVi... 查看详情
将部分和行添加到 UITableView 以重置表
】将部分和行添加到UITableView以重置表【英文标题】:AddSectionandRowtoUITableViewtoResetTable【发布时间】:2012-05-1116:31:04【问题描述】:我有一个UITableView,用户可以构建它并向其添加行。在编辑表格视图时,我想添加一个带有单个红... 查看详情
将节中的列表样式从节的父级删除后带回
Weuselistsforallsortsofthings,especiallnavigationelementsforwhichbulletsdon'tmakesense.Often,itismostconvenienttoremovebullet/liststylesfromalllistelementsglobally.Infactmanycssresetsdojustthat.However,theremaybecontentareaswherewedowantbullets!Thefollowingcssbringsbacktheliststylesforalllistel... 查看详情
添加行后如何更新 UITableView?
】添加行后如何更新UITableView?【英文标题】:HowtoupdateUITableViewafteraddingarow?【发布时间】:2014-08-3104:37:09【问题描述】:我有一个非常简单的UITableView。我在这里通过创建一些对象来初始化它并将它们添加到完美的表格视图中:... 查看详情
如何在 UITableView 中显示零行的表格
】如何在UITableView中显示零行的表格【英文标题】:howtodisplayatablewithzerorowsinUITableView【发布时间】:2009-09-2907:13:32【问题描述】:我正在动态地将内容加载到UITableView中。如果有数据,表格需要显示数据。如果没有数据,表格应... 查看详情
文本编辑后 UITableView 在更新时崩溃
】文本编辑后UITableView在更新时崩溃【英文标题】:UITableViewcrashonupdatesaftertextedit【发布时间】:2010-07-0520:15:03【问题描述】:所以我完全被这个难住了,很想称之为“操作系统错误”。我有一个带有单个部分的TableView控制器,... 查看详情
添加 UISearchController 后 UITableView 可以滚动到顶部太远
】添加UISearchController后UITableView可以滚动到顶部太远【英文标题】:UITableViewcanbescrolledtoofartopafteraddingUISearchController【发布时间】:2018-04-1408:13:05【问题描述】:在我使用下面的代码将UISearchController添加到UITableView之后。lazyvarsearc... 查看详情
删除带有节标题的表视图中的行
...ctionheaders【发布时间】:2013-10-1519:25:37【问题描述】:在将节标题添加到我的应用程序中的一个表之前,我能够使用commitEditingStyle函数删除行。我决定实现部分标题,以便用户更轻松地查看按日期添加到表中的项目。我测试了这... 查看详情