I'm trying to get the the number of cells in uitableview because I want to print this number in another viewcontroller. I have tried some things but anything works on iOS 8. The only function that returns the number of cells is:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection (NSInteger)section {
// Return the number of rows in the section.
return clases.count;
}
(I have checked printing the clases.count with NSLog).
But I don't know hot use the returned value in the viewDidLoad method or in other viewcontroller.
My code from ClasesTableViewController is:
#import "ClasesTableViewController.h"
#import "ClasesTableViewCell.h"
@interface ClasesTableViewController ()
@end
@implementation ClasesTableViewController
@synthesize clases;
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationItem setTitle:@"Clases"]; /*Cambia el titulo del navigation controller*/
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; /*Cambia el color de las letras del navigation controller bar del menu principal*/
/*[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:50.0f/255.0f green:72.0f/255.0f blue:159.0f/255.0f alpha:1.0f]];*/ /*Cambia el color del navigation controller bar del menu principal*/
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:72/255.0f green:133/255.0f blue:50/255.0f alpha:1.0f]];
self.TableView.tableFooterView = [[UIView alloc] init]; /*Esta linea hace que en la tabla solo aparezcan el numero de filas que tienes establecidas, es decir, que las vacias no aparezcan*/
self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; /*Cambia el color del boton de la izquierda*/
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"UpdateClase"]) {
NSManagedObject *selectedClase = [clases objectAtIndex:[[self.TableView indexPathForSelectedRow] row]];
ClasesViewController *destViewController = segue.destinationViewController;
destViewController.clase = selectedClase;
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSManagedObjectContext *moc = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Clases"];
clases = [[moc executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return clases.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
/*UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];*/
ClasesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//Configure the cell
NSManagedObject *clase = [clases objectAtIndex:indexPath.row];
[cell.dateLabel setText:[NSString stringWithFormat:@"%@", /*countString,*/ [clase valueForKey:@"date"]/*, [clase valueForKey:@"date"]*/]];
[cell.timeLabel setText:[clase valueForKey:@"time"]];
[cell.paidLabel setText:[clase valueForKey:@"paid"]];
cell.paidImage.image = [UIImage imageNamed:@" "];
if ([cell.paidLabel.text isEqualToString:@"No"] || [cell.paidLabel.text isEqualToString:@"no"]) {
cell.paidLabel.textColor = [UIColor redColor];
}
if ([cell.paidLabel.text isEqualToString:@"Si"] || [cell.paidLabel.text isEqualToString:@"si"]) {
cell.paidLabel.textColor = [UIColor greenColor];
cell.paidImage.image =[UIImage imageNamed:@"paid_green@2x.png"];
}
return cell;
}
// 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 {
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[context deleteObject:[clases objectAtIndex:indexPath.row]];
//invoke the "save" method to commit the change
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}
//Remove clase from table view
[clases removeObjectAtIndex:indexPath.row];
[self.TableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Can anyone help me? Thank you very much.
Aucun commentaire:
Enregistrer un commentaire