【问题标题】:Get the number of cells in uitableview iOS 8 xcode获取uitableview iOS 8 xcode中的单元格数
【发布时间】:2015-03-24 16:26:01
【问题描述】:

我正在尝试获取 uitableview 中的单元格数量,因为我想在另一个视图控制器中打印此数字。我尝试了一些东西,但在 iOS 8 上一切正常。返回单元格数量的唯一函数是:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection (NSInteger)section {
    // Return the number of rows in the section.
    return clases.count;
}

(我已经检查了使用 NSLog 打印 clases.count)。

但我不知道在viewDidLoad方法或其他viewcontroller中热使用返回值。

我的 ClassesTableViewController 代码是:

#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];

     }
 }

谁能帮助我?非常感谢。

【问题讨论】:

    标签: ios iphone xcode uitableview swift


    【解决方案1】:

    试试看:

    - (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;
               destViewController.numberOfClasses = clases.count;
           }
        }
    

    Ps.:ClasesViewController 应该有一个名为 numberOfClasses 的公共属性

    【讨论】:

      猜你喜欢
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      相关资源
      最近更新 更多