UITableView detail transition

2019-09-04 12:41发布

问题:

I'm tryin to do a detail list but i can't do it and i don't understand why.

Is a storyboard, the storyboard is connected ok. Is only one tableView and View controller for detail.

I'm trying to push in one cell and go to the detail screen and at the detail class i've two propertys but when i push in a cell, the app crash and i've a exception:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'GasolDetalleVCViewController'' *** First throw call stack:

The classes are:

Table Controller .h:

#import <UIKit/UIKit.h>
#import "GasolDetalleVCViewController.h"
#import "DetallesGasol.h"

@interface GasolTVCTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray *arrayNombrePrecio;
    NSInteger celdaPulsada;
}
@end

Table Controller .m:

#import "GasolTVCTableViewController.h"

@interface GasolTVCTableViewController ()

@end

@implementation GasolTVCTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    arrayNombrePrecio = [NSMutableArray arrayWithCapacity:10];

    DetallesGasol *textoCelda0 = [[DetallesGasol alloc]init];
    DetallesGasol *textoCelda1 = [[DetallesGasol alloc]init];
    DetallesGasol *textoCelda2 = [[DetallesGasol alloc]init];
    DetallesGasol *textoCelda3 = [[DetallesGasol alloc]init];
    DetallesGasol *textoCelda4 = [[DetallesGasol alloc]init];
    DetallesGasol *textoCelda5 = [[DetallesGasol alloc]init];
    DetallesGasol *textoCelda6 = [[DetallesGasol alloc]init];
    DetallesGasol *textoCelda7 = [[DetallesGasol alloc]init];
    DetallesGasol *textoCelda8 = [[DetallesGasol alloc]init];
    DetallesGasol *textoCelda9 = [[DetallesGasol alloc]init];

    textoCelda0.nombre = @"Autogas Alcorcon";
    textoCelda0.precio = 0.71;
    textoCelda0.latitud = 40.3427359;
    textoCelda0.longitud = -3.8053231000000096;
    [arrayNombrePrecio addObject:textoCelda0];

    textoCelda1.nombre = @"Autogas Atalayuela Vallecas";
    textoCelda1.precio = 0.72;
    textoCelda1.latitud = 40.3707274;
    textoCelda1.longitud = -3.643952000000013;
    [arrayNombrePrecio addObject:textoCelda1];

    textoCelda2.nombre = @"GLP Santa Eugenia";
    textoCelda2.precio = 0.702;
    textoCelda2.latitud = 40.3831964;
    textoCelda2.longitud = -3.6279809000000114;
    [arrayNombrePrecio addObject:textoCelda3];

    textoCelda3.nombre = @"Autogas Hispanidad";
    textoCelda3.precio = 0.73;
    textoCelda3.latitud = 40.4678932;
    textoCelda3.longitud = -3.57114660000002;
    [arrayNombrePrecio addObject:textoCelda3];

    textoCelda4.nombre = @"Autogas Mendez Alvaro";
    textoCelda4.precio = 0.72;
    textoCelda4.latitud = 40.3911409;
    textoCelda4.longitud = -3.6750798000000486;
    [arrayNombrePrecio addObject:textoCelda4];

    textoCelda5.nombre = @"Autogas Hipódromo";
    textoCelda5.precio = 0.70;
    textoCelda5.latitud = 40.4635268;
    textoCelda5.longitud = -3.7577165999999806;
    [arrayNombrePrecio addObject:textoCelda5];

    textoCelda6.nombre = @"Autogas Las Tablas";
    textoCelda6.precio = 0.71;
    textoCelda6.latitud = 40.5074619;
    textoCelda6.longitud =  -3.6695012000000133;
    [arrayNombrePrecio addObject:textoCelda6];

    textoCelda7.nombre = @"GLP Alcorcon";
    textoCelda7.precio = 0.73;
    textoCelda7.latitud = 40.3440681;
    textoCelda7.longitud = -3.838870799999995;
    [arrayNombrePrecio addObject:textoCelda7];

    textoCelda8.nombre = @"Autogas Las Rozas";
    textoCelda8.precio = 0.72;
    textoCelda8.latitud = 40.50090272885034;
    textoCelda8.longitud = -3.8722419196654982;
    [arrayNombrePrecio addObject:textoCelda8];

    textoCelda9.nombre = @"Autogas Pinto";
    textoCelda9.precio = 0.72;
    textoCelda9.latitud = 40.2550693;
    textoCelda9.longitud =  -3.69782639999994;
    [arrayNombrePrecio addObject:textoCelda9];

}

- (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 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"celda"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"celda"];
    }

    DetallesGasol *detalleAux = [arrayNombrePrecio objectAtIndex:indexPath.row];
    NSString *aux = [NSString stringWithFormat:@"%.3f", detalleAux.precio];

    cell.textLabel.text = detalleAux.nombre;
    cell.detailTextLabel.text = aux;

    cell.textLabel.minimumFontSize = 12;
    cell.detailTextLabel.minimumFontSize = 12;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GasolDetalleVCViewController *detailVC = [[GasolDetalleVCViewController alloc]initWithNibName:@"GasolDetalleVCViewController" bundle:nil];

    DetallesGasol *detAux = [arrayNombrePrecio objectAtIndex:indexPath.row];
    NSString *strAuxLat =  [NSString stringWithFormat:@"%f", detAux.latitud];
    NSString *strAuxLon =  [NSString stringWithFormat:@"%f", detAux.longitud];

    detailVC.latitud = strAuxLat;
    detailVC.longitud = strAuxLon;

    [self.navigationController pushViewController:detailVC animated:YES];
}

//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//    // Get the new view controller using [segue destinationViewController].
//    // Pass the selected object to the new view controller.
//    
////    GasolDetalleVCViewController *gasol = segue.destinationViewController;
////    gasol.detalle = [arrayNombrePrecio objectAtIndex:celdaPulsada];
//    
//}

@end

Array class object .h:

#import <Foundation/Foundation.h>

@interface DetallesGasol : NSObject

@property (strong, nonatomic) NSString *nombre;
@property (assign) CGFloat precio;
@property (assign) CGFloat latitud;
@property (assign) CGFloat longitud;

@end

Array class object .m:

#import "DetallesGasol.h"

@implementation DetallesGasol

@end

The Detail Class controller .h:

#import <UIKit/UIKit.h>

@interface GasolDetalleVCViewController : UIViewController

@property (strong, nonatomic) NSString *latitud;
@property (strong, nonatomic) NSString *longitud;

@end

The Detail Class controller .m:

#import "GasolDetalleVCViewController.h"

@interface GasolDetalleVCViewController ()

@end

@implementation GasolDetalleVCViewController
@synthesize latitud, longitud;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSLog(@"Entra en viewDidLoad de GasolDetalleViewController");
    NSLog(@"latitud: %@", latitud);
    NSLog(@"longitud: %@", latitud); 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

Thanks a lot

回答1:

This error can occure when you rename some files outside XCode. To solve it you can just remove the files from your project (Right Click - Delete and "Remove Reference") You re-import the files in your project and everything will be ok !