Ios 将行数放入节标题中
我需要在小节标题中包括行数,比如今天(6),这意味着在今天标题的小节中有6行(在我的例子中是核心数据对象)。我知道在这里可以找到:Ios 将行数放入节标题中,ios,uitableview,Ios,Uitableview,我需要在小节标题中包括行数,比如今天(6),这意味着在今天标题的小节中有6行(在我的例子中是核心数据对象)。我知道在这里可以找到: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsControll
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
id sectionInfo=[[self.fetchedResultsController节]objectAtIndex:section];
返回[sectionInfo numberOfObjects];
}
但是我不知道如何在每一个部分中都包含这个标签文本。
欢迎任何建议
#import "ToDoItemsTableViewController.h"
#import "AppDelegate.h"
#import "AddToDoItemViewController.h"
#import "ToDoSubItemsTableViewController.h"
@interface ToDoItemsTableViewController ()
@property (nonatomic, strong)NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong)NSFetchedResultsController *fetchedResultsController;
@end
@implementation ToDoItemsTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(NSManagedObjectContext *)managedObjectContext{
return [(AppDelegate*)[[UIApplication sharedApplication]delegate]managedObjectContext];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *error = nil;
if (![[self fetchedResultsController]performFetch:&error]){
NSLog(@"Error %@",error);
abort();
}
}
-(void) viewWillAppear:(BOOL)animated{
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier]isEqualToString:@"addToDoItem"]){
UINavigationController *navigationController = segue.destinationViewController;
AddToDoItemViewController *addToDoItemViewController = (AddToDoItemViewController*)navigationController.topViewController;
ToDoItem *addToDoItem = [NSEntityDescription insertNewObjectForEntityForName:@"ToDoItem" inManagedObjectContext:self.managedObjectContext];
addToDoItemViewController.addToDoItem = addToDoItem;
}
if ([[segue identifier] isEqualToString:@"toToDoSubItems"]){
ToDoSubItemsTableViewController *todoSubItemsTableViewController = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
ToDoItem *selectedToDoItem = (ToDoItem*)[self.fetchedResultsController objectAtIndexPath:indexPath];
todoSubItemsTableViewController.selectedToDoItem = selectedToDoItem;
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections]count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
ToDoItem *todoItem = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = todoItem.todoName;
NSDate *fechaToDO = todoItem.todoDueDate;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"EEEE, dd MMMM YYYY"];
NSString *fechaToDo = [dateFormatter stringFromDate:fechaToDO];
cell.detailTextLabel.text = fechaToDo;
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *header = @"customHeader";
UITableViewHeaderFooterView *vHeader;
vHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:header];
if (!vHeader) {
vHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:header];
vHeader.textLabel.backgroundColor = [UIColor redColor];
vHeader.textLabel.textColor = [UIColor whiteColor];
vHeader.contentView.backgroundColor = [UIColor redColor];
}
if (section == 1) {
vHeader.textLabel.backgroundColor = [UIColor blueColor];
vHeader.textLabel.textColor = [UIColor whiteColor];
vHeader.contentView.backgroundColor = [UIColor blueColor];
} else {
[vHeader setBackgroundColor:[UIColor redColor]];
}
vHeader.textLabel.text = [self tableView:tableView titleForHeaderInSection:section];
return vHeader;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections]objectAtIndex:section];
NSString *sectionname = [theSection name];
if ([sectionname isEqualToString:@"0"]){
return @"Overdue";
}
else if ([sectionname isEqualToString:@"1"]){
return @"Today";
}
else if ([sectionname isEqualToString:@"2"]){
return @"Upcoming";
}
if ([[self.fetchedResultsController sections]count]>0){
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
return [sectionInfo name];
}
else{
return nil;
}
}
#pragma mark - Fetched Results Controller Section
-(NSFetchedResultsController*)fetchedResultsController{
if (_fetchedResultsController != nil){
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSManagedObjectContext *context = self.managedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ToDoItem" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"todoDueDate" ascending:YES];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc]initWithKey:@"todoName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor,sortDescriptor1, nil];
fetchRequest.sortDescriptors = sortDescriptors;
_fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:@"sectionIdentifier" cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
#pragma mark - Fetched Results Controller Delegates
-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
-(void) controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{
UITableView *tableView = self.tableView;
switch (type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:{
ToDoItem *changeToDoItem = [self.fetchedResultsController objectAtIndexPath:indexPath];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = changeToDoItem.todoName;
cell.detailTextLabel.text = changeToDoItem.todoDescription;
}
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
-(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type{
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
NSManagedObjectContext *context = [self managedObjectContext];
ToDoItem *ToDoItemToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
[context deleteObject:ToDoItemToDelete];
NSError *error = nil;
if (![context save:&error]){
NSLog(@"Error: %@",error);
}
}
}
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#导入“ToDoItemsTableViewController.h”
#导入“AppDelegate.h”
#导入“AddToDoItemViewController.h”
#导入“ToDosAbitemStableViewController.h”
@与DoitemStableViewController()的接口
@属性(非原子,强)NSManagedObjectContext*managedObjectContext;
@属性(非原子,强)NSFetchedResultsController*fetchedResultsController;
@结束
@DoitemStableViewController的实现
-(id)initWithStyle:(UITableViewStyle)样式
{
self=[super initWithStyle:style];
如果(自我){
//自定义初始化
}
回归自我;
}
-(NSManagedObjectContext*)managedObjectContext{
返回[(AppDelegate*)[[UIApplication sharedApplication]委托]managedObjectContext];
}
-(无效)viewDidLoad
{
[超级视图下载];
n错误*错误=nil;
如果(![[self-fetchedResultsController]性能检测:&错误]){
NSLog(@“错误%@”,错误);
中止();
}
}
-(无效)视图将显示:(BOOL)动画{
[self.tableView重载数据];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方{
如果([[segue identifier]IsequalString:@“addToDoItem”]){
UINavigationController*navigationController=segue.destinationViewController;
AddToDoItemViewController*AddToDoItemViewController=(AddToDoItemViewController*)navigationController.topViewController;
ToDoItem*addToDoItem=[NSEntityDescription insertNewObjectForEntityForName:@“ToDoItem”在managedObjectContext:self.managedObjectContext]中;
addToDoItemViewController.addToDoItem=addToDoItem;
}
如果([[segue identifier]isEqualToString:@“ToToToDoSubItems”]){
TODOSUBITEMSTABLEVEWCONTROLLER*TODOSUBITEMSTABLEVEWCONTROLLER=[segue destinationViewController];
NSIndexPath*indexPath=[self.tableView indexPathForSelectedRow];
ToDoItem*selectedToDoItem=(ToDoItem*)[self.fetchedResultsController对象索引路径:indexPath];
ToDosBitemStableViewController.selectedToDoItem=selectedToDoItem;
}
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回[[self.fetchedResultsController节]计数];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
id sectionInfo=[[self.fetchedResultsController节]objectAtIndex:section];
返回[sectionInfo numberOfObjects];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//配置单元格。。。
ToDoItem*ToDoItem=[self.fetchedResultsController对象索引路径:indexPath];
cell.textlab.text=todoItem.todoName;
NSDate*fechaToDO=todoItem.todoDueDate;
NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];
[日期格式化程序setDateFormat:@“EEEE,dd-MMMM-YYYY”];
NSString*fechaToDo=[dateFormatter stringFromDate:fechaToDo];
cell.detailTextLabel.text=fechaToDo;
返回单元;
}
-(UIView*)表格视图:(UITableView*)表格视图用于标题部分:(NSInteger)部分
{
静态NSString*头=@“customHeader”;
UITableViewHeaderFooterView*vHeader;
vHeader=[tableView dequeueReusableHeaderFooterViewWithIdentifier:header];
如果(!vHeader){
vHeader=[[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:header];
vHeader.textLabel.backgroundColor=[UIColor redColor];
vHeader.textLabel.textColor=[UIColor whiteColor];
vHeader.contentView.backgroundColor=[UIColor redColor];
}
如果(节==1){
vHeader.textLabel.backgroundColor=[UIColor blueColor];
vHeader.textLabel.textColor=[UIColor whiteColor];
vHeader.contentView.backgroundColor=[UIColor blueColor];
}否则{
[vHeader setBackgroundColor:[UIColor redColor]];
}
vHeader.textlab.text=[self tableView:tableView titleForHeaderInSection:section];
返回vHeader;
}
-(NSString*)表格视图:(UITableView*)表格视图标题标题标题部分:(NSInteger)部分{
id theSection=[[self.fetchedResultsController节]objectAtIndex:section];
NSString*sectionname=[theSection name];
如果([sectionname isEqualToString:@“0”]){
返回@“过期”;
}
else if([sectionname IsequalString:@“1”]){
返回"今日",;
}
else if([sectionname IsequalString:@“2”]){
返回@“即将到来”;
}
如果([[self.fetchedResultsController节]计数]>0){
id sectionInfo=[[self.fetchedResultsController节]objectAtIndex:section];
返回[sectionInfo name];
}
否则{
返回零;
}
}
#pragma标记-获取的结果控制器部分
-(NSFetchedResultsController*)fetchedResultsController{
如果(_fetchedResultsController!=nil){
返回_fetchedResultsController;
}
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init];
NSManagedObjectContext*context=self.managedObjectContext;
NSEntityDescription*entity=[NSEntityDescription entityForName:@“ToDoItem”在ManagedObjectContext:context中];
[FetchRequestSetEntity:entity];
NSSortDescr
[NSString stringWithFormat:@"Today(%d)", [tableView numberOfRowsInSection:
section]];