Core data 在动态表格视图中显示单元格总数
我有一个问题,我相信很多objectiveC开发人员都能很容易地解决。我正在开发一个应用程序,它是一个动态的tableview,用户可以创建不同的组。用户可以输入具有不同总数的多个组。我试图将创建的所有组的总数相加 我用来计算小计的IBAction是Core data 在动态表格视图中显示单元格总数,core-data,nsarray,core-data-migration,Core Data,Nsarray,Core Data Migration,我有一个问题,我相信很多objectiveC开发人员都能很容易地解决。我正在开发一个应用程序,它是一个动态的tableview,用户可以创建不同的组。用户可以输入具有不同总数的多个组。我试图将创建的所有组的总数相加 我用来计算小计的IBAction是 - (IBAction)Calculate:(id)sender { //--------------- NSInteger Tempsum = [[_dataArray valueForKeyPath:@"@sum.name2"] intege
- (IBAction)Calculate:(id)sender {
//---------------
NSInteger Tempsum = [[_dataArray valueForKeyPath:@"@sum.name2"] integerValue];
_TotalAnimals.text = [[NSString alloc] initWithFormat:@"%.1ld", (long)Tempsum];
//--------------
}
我在tableview的.m文件中创建了以下代码,还创建了名为的NSArray属性;
@属性NSArray*数据数组
但是,我不确定如何使用正确的代码将结果@@sum.name2链接到Tableviewcontroller_TotalAnimals.text中的文本值
对此问题的任何帮助都将不胜感激。下面是.m文件的完整代码
导入TSPViewController.h
进口
导入TSPToDoCell.h
导入tspaddtoProviewController.h
导入tspupdatetooviewcontroller.h
int TENTOTALANIMALS=0;
整数=0;
int Tempsum=0;
@接口TSPViewController
@属性强,非原子NSFetchedResultsController*fetchedResultsController;
@属性强,非原子NSIndexPath*选择;
@属性NSArray*数据数组;
@结束
@TSPViewController的实现
布拉格标记-
pragma标记视图生命周期
-空隙率{
[超级视图下载];
//初始化提取请求
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]initWithEntityName:@TSPItem];
//添加排序描述符
[fetchRequest setSortDescriptors:@[[NSSortDescriptor SortDescriptor WithKey:@createdAt升序:是]];
//初始化获取的结果控制器
self.fetchedResultsController=[[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil缓存名称:nil];
//配置获取的结果控制器
[self.fetchedResultsController设置委托:self];
//执行提取
n错误*错误=nil;
[self.fetchedResultsController性能蚀刻:&错误];
如果错误{
NSLog@Unable执行提取操作。;
NSLog@%@、%@,错误,错误.localizedDescription;
}
}
-voidprepareForSegue:UIStoryboardSegue*序列发送方:idsender{
如果[segue.identifier IsequalString:@addToProviewController]{
//获取对视图控制器的引用
UINavigationController*nc=UINavigationController*[segue destinationViewController];
TSPAddToDoViewController*vc=TSPAddToDoViewController*[nc topViewController];
//配置视图控制器
[vc setManagedObjectContext:self.managedObjectContext];
}如果[segue.identifier isEqualToString:@updateToDoViewController],则为else{
//获取对视图控制器的引用
TSPUpdateToDoViewController*vc=tspupdatetooviewcontroller*[segue destinationViewController];
//配置视图控制器
[vc setManagedObjectContext:self.managedObjectContext];
如果自我选择{
//获取记录
NSManagedObject*record=[self.fetchedResultsController对象索引路径:self.selection];
如果记录{
[vc setRecord:record];
}
//重置选择
[自选:无];
}
}
}
布拉格标记-
pragma标记获取的结果控制器委托方法
-voidcontrollerWillChangeContent:NSFetchedResultsController*控制器{
[self.tableView开始更新];
}
-voidControllerdChangeContent:NSFetchedResultsController*控制器{
[self.tableView endUpdates];
}
-voidcontroller:NSFetchedResultsController*控制器didChangeObject:idanObject atIndexPath:NSIndexPath*indexPath forChangeType:nsFetchedResultsSchangeType newIndexPath:NSIndexPath*newIndexPath{
开关类型{
案例NSFetchedResultsChangesInsert:{
[self.tableView insertRowsAtIndexPaths:[NSArray arraywhithobject:newindexath]with rownanimation:uitableviewrownanimationfade];
打破
}
案例NSFetchedResultsChangeDelete:{
[self.tableView deleteRowsatindExpath:[NSArray arrayWithObject:indexPath]带RowAnimation:UITableViewRowAnimationFade];
打破
}
案例NSFetchedResultsChangeUpdate:{
[self-configureCell:TSPToDoCell*[self.tableView CellForRowatineXpath:indexPath]AtiXpath:indexPath];
打破
}
案例NSFetchedResultsChangeMove:{
[self.tableView deleteRowsatindExpath:[NSArray arrayWithObject:indexPath]带RowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:[NSArray arraywhithobject:newindexath]with rownanimation:uitableviewrownanimationfade];
打破
}
}
}
布拉格标记-
pragma标记表视图数据源方法
-NSIntegernumberOfSectionsInTableView:UITableView*tableV
观点{
返回[[self.fetchedResultsController节]计数];
}
-voidconfigureCell:TSPToDoCell*单元格索引路径:NSIndexPath*indexPath{
//获取记录
NSManagedObject*record=[self.fetchedResultsController对象索引路径:indexPath];
//更新单元
[cell.namelab setText:[记录值forkey:@name]];
[cell.nameLabel2 setText:[记录值forkey:@name2]];
[cell.doneButton setSelected:[[record valueForKey:@done]布尔值];
[单元格设置DidTapButtonBlock:^{
BOOL isDone=[[record valueForKey:@done]boolValue];
//更新记录
[记录设置值:@!isDone forKey:@done];
}];
}
-BOOLtableView:UITableView*tableView caneditrowatinexpath:nsindepath*indepath{
返回YES;
}
-BOOLtableView:UITableView*tableView可以移动rowatinexpath:nsindepath*indepath{
返回否;
}
-voidtableView:UITableView*tableView提交的编辑样式:UITableViewCellEditingStyleeditingStyle for Row索引路径:NSIndexPath*indexPath{
如果editingStyle==UITableViewCellEditingStyleDelete{
NSManagedObject*record=[self.fetchedResultsController对象索引路径:indexPath];
如果记录{
[self.fetchedResultsController.managedObjectContext删除对象:记录];
}
}
}
布拉格标记-
pragma标记表视图委托方法
-voidtableView:UITableView*tableView didSelectRowatineXpath:NSIndexPath*indexPath{
[tableView取消行索引路径:indexPath动画:是];
//商店选择
[自选:indexPath];
//表演序曲
[self-PerformsgueWithIdentifier:@updateToDoViewController发件人:self];
}
//在下面============
-NSIntegertableView:UITableView*表格视图行数截面:NSIntegersection{
NSArray*节=[self.fetchedResultsController节];
id sectionInfo=[sections objectAtIndex:section];
返回[sectionInfo numberOfObjects];
}
-UITableViewCell*tableView:UITableView*tableView cellForRowAtIndexPath:NSIndexPath*indexPath{
TSPToDoCell*cell=TSPToDoCell*[tableView dequeueReusableCellWithIdentifier:@ToDoCell forIndexPath:indexPath];
//配置表视图单元格
[self-configureCell:cell-atIndexPath:indexPath];
返回单元;
}
-IBActionCalculate:idsender{
//--------
NSInteger Tempsum=[[u dataArray valueForKeyPath:@@sum.name2]integerValue];
_TotalAnimals.text=[[NSString alloc]initWithFormat:@%.1ld,longTempsum];
//-------
}
/*
//--------
NSInteger TempsumTotalAnimals=[[u charactersArray valueForKeyPath:@@sum.passenger]integerValue];
_TotalAnimals.text=[[NSString alloc]initWithFormat:@%.1ld,longTempsumTotalAnimals];
//-------
*/
@End您的代码段为_TotalAnimals.text赋值,它是如何不按预期工作的?我很确定为_TotalAnimals.text赋值的代码段是正确的,但是,当我提到属性_dataArray时,我需要以某种方式链接_dataArray或某种类型的NSArray或链接到函数@sum.name2,我想我的问题的关键是,假设_dataArray不正确,我如何让NSInteger Tempsum等于@sum.name2所持有的信息