1

我还是编程新手。我正在为我的 iOS 开发课程做最后一个项目,它是构建一个基本的应用程序。几天来,我一直试图弄清楚如何完成这项工作,这是一个接一个的问题。现在我时间紧迫,因为明天到期。我的应用程序应该显示雪佛兰​​汽车列表和每个汽车名称旁边的图片。这发生在 Table View Controller 中。我的问题在于单击单元格时。汽车信息的标签是空白的。例如,我有一个标签,上面写着“马力”,在它旁边,它应该显示汽车的实际马力。它不显示实际马力,而是显示一个空白标签。有什么我想念的吗?有没有办法向你们展示我使用的代码?我只有“1”的声誉。

任何帮助,将不胜感激。

这是来自 tableviewcontroller:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    static NSString *CellIdentifier = @"carTableCell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];



    cell.textLabel.text =  [_carModels objectAtIndex: indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[_carImages objectAtIndex: indexPath.row]];

         return cell;
}

这是我的 detailViewController.m

#import "Chevy2DetailViewController.h"

@interface Chevy2DetailViewController ()

@end

@implementation Chevy2DetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _modelLabel.text = _currentCarDetail.carModel;
    _MSRPLabel.text = _currentCarDetail.carMSRP;
    _MPGLabel.text = _currentCarDetail.carMPG;
    _airbagLabel.text = _currentCarDetail.carAirbag;
    _volumeLabel.text = _currentCarDetail.carVolume;
    _horsepowerLabel.text = _currentCarDetail.carHorsepower;
    _bigImage.image = [UIImage imageNamed:_currentCarDetail.thumbImageName];

}

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

@end

这是 TableViewController.m 文件:

//
//  Chevy2TableViewController.m
//  Chevy2
//
//  Created by Anthony Picone on 7/16/13.
//  Copyright (c) 2013 Pico. All rights reserved.
//

#import "Chevy2TableViewController.h"
#import "Chevy2TableViewCell.h"
#import "CarDetail.h"
#import "Chevy2DetailViewController.h"

@interface Chevy2TableViewController ()

@end

@implementation Chevy2TableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];




  _carModels = @[@"Spark",
             @"Sonic",
             @"Cruze",
             @"Volt",
             @"Malibu",
             @"Impala",
             @"SS",
             @"Camaro",
             @"Corvette",
             @"Equinox",
             @"Traverse",
             @"Tahoe",
             @"Suburban",
             @"Avalanche",
             @"Silverado"];

_carImages = @[@"spark.png",
               @"Sonic.png",
               @"Cruze.png",
               @"Volt.png",
               @"Malibu.png",
               @"Impala.png",
               @"SS.png",
               @"Camaro.png",
               @"corvette.png",
               @"Equinox.png",
               @"Traverse.png",
               @"Tahoe.png",
               @"Suburban.png",
               @"avalanche.png",
               @"silverado.png"];



CarDetail *sparkCarDetail = [CarDetail new];
sparkCarDetail.carModel = @"Spark";
sparkCarDetail.carMSRP = @"$12,170";
sparkCarDetail.carMPG = @"38 MPG HWY";
sparkCarDetail.carAirbag= @"10 Airbags";
sparkCarDetail.carVolume = @"31.2 Cubic Feet";
sparkCarDetail.carHorsepower = @"84 HP";
sparkCarDetail.bigImage = @"big_spark.jpg";


CarDetail *sonicCarDetail = [CarDetail new];
sonicCarDetail.carModel = @"Sonic";
sonicCarDetail.carMSRP = @"$14,185";
sonicCarDetail.carMPG = @"40 MPG HWY";
sonicCarDetail.carAirbag= @"10 Airbags";
sonicCarDetail.carVolume = @"14.9 Cubic Feet";
sonicCarDetail.carHorsepower = @"138";
sonicCarDetail.bigImage = @"big_sonic.jpg";

CarDetail *cruzeCarDetail = [CarDetail new];
cruzeCarDetail.carModel = @"Cruze";
cruzeCarDetail.carMSRP = @"$17,170";
cruzeCarDetail.carMPG = @"46 MPG HWY";
cruzeCarDetail.carAirbag = @"10 Airbags";
cruzeCarDetail.carVolume = @"15.4 Cubic Feet";
cruzeCarDetail.carHorsepower = @"138 HP";
cruzeCarDetail.bigImage = @"big_cruze.jpg";

CarDetail *voltCarDetail = [CarDetail new];
voltCarDetail.carModel = @"Volt";
voltCarDetail.carMSRP = @"$39,145";
voltCarDetail.carMPG = @"101 MPG CTY";
voltCarDetail.carAirbag = @"8 Airbags";
voltCarDetail.carVolume = @"n/a";
voltCarDetail.carHorsepower = @"149 HP";
voltCarDetail.bigImage = @"big_volt.jpg";

CarDetail *malibuCarDetail = [CarDetail new];
malibuCarDetail.carModel = @"Malibu";
malibuCarDetail.carMSRP = @"$21,995";
malibuCarDetail.carMPG = @" 37 MPG HWY";
malibuCarDetail.carAirbag = @"10 Airbags";
malibuCarDetail.carVolume = @"13.2 Cubic Feet";
malibuCarDetail.carHorsepower = @"259 HP";
malibuCarDetail.bigImage = @"big_malibu.jpg";

CarDetail *impalaCarDetail = [CarDetail new];
impalaCarDetail.carModel = @"Impala";
impalaCarDetail.carMSRP = @"$26,725";
impalaCarDetail.carMPG = @"31 MPG HWY";
impalaCarDetail.carAirbag= @"10 Airbags";
impalaCarDetail.carVolume = @"18.8 Cubic Feet";
impalaCarDetail.carHorsepower = @"305 HP";
impalaCarDetail.bigImage = @"big_impala.jpg";

CarDetail *ssCarDetail = [CarDetail new];
ssCarDetail.carModel = @"SS";
ssCarDetail.carMSRP = @"n/a";
ssCarDetail.carMPG = @"n/a";
ssCarDetail.carAirbag = @"n/a";
ssCarDetail.carVolume = @"n/a";
ssCarDetail.carHorsepower = @"415 HP";
ssCarDetail.bigImage = @"big_ss.jpg";

CarDetail *camaroCarDetail = [CarDetail new];
camaroCarDetail.carModel = @"Camaro";
camaroCarDetail.carMSRP = @"$23,345";
camaroCarDetail.carMPG = @"28 MPG HWY";
camaroCarDetail.carAirbag = @"6 Airbags";
camaroCarDetail.carVolume = @"11.3 Cubic Feet";
camaroCarDetail.carHorsepower= @"323 HP";
camaroCarDetail.bigImage = @"big_camaro.jpg";

CarDetail *corvetteCarDetail = [CarDetail new];
corvetteCarDetail.carModel = @"Corvette";
corvetteCarDetail.carMSRP = @"$49,600";
corvetteCarDetail.carMPG = @"26 MPG HWY";
corvetteCarDetail.carAirbag = @"4 Airbags";
corvetteCarDetail.carVolume = @"11.0 Cubic Feet";
corvetteCarDetail.carHorsepower = @"430 HP";
corvetteCarDetail.bigImage = @"big_corvette.jpg";

CarDetail *equinoxCarDetail = [CarDetail new];
equinoxCarDetail.carModel = @"Equinox";
equinoxCarDetail.carMSRP = @"$24,225";
equinoxCarDetail.carMPG = @"32 MPG HWY";
equinoxCarDetail.carAirbag = @"6 Airbags";
equinoxCarDetail.carVolume = @"63.7 Cubic Feet";
equinoxCarDetail.carHorsepower = @"182 HP";
equinoxCarDetail.bigImage = @"big_equinox";

CarDetail *traverseCarDetail = [CarDetail new];
traverseCarDetail.carModel = @"Traverse";
traverseCarDetail.carMSRP = @"$30,795";
traverseCarDetail.carMPG = @"24 MPG HWY";
traverseCarDetail.carAirbag = @"6 Airbags";
traverseCarDetail.carVolume = @"116.3 Cubic Feet";
traverseCarDetail.carHorsepower = @"281 HP";
traverseCarDetail.bigImage = @"big_traverse";

CarDetail *tahoeCarDetail = [CarDetail new];
tahoeCarDetail.carModel = @"Tahoe";
tahoeCarDetail.carMSRP = @"$40,405";
tahoeCarDetail.carMPG = @"21 MPG HWY";
tahoeCarDetail.carAirbag = @"6 Airbags";
tahoeCarDetail.carVolume = @"108.9 Cubic Feet";
tahoeCarDetail.carHorsepower = @"320 HP";
tahoeCarDetail.bigImage = @"big_tahoe";

CarDetail *suburbanCarDetail = [CarDetail new];
suburbanCarDetail.carModel = @"Suburban";
suburbanCarDetail.carMSRP = @"$43,870";
suburbanCarDetail.carMPG = @"21 MPG HWY";
suburbanCarDetail.carAirbag = @"6 Airbags";
suburbanCarDetail.carVolume = @"137.4";
suburbanCarDetail.carHorsepower= @"320 HP";
suburbanCarDetail.bigImage = @"big_suburban";

CarDetail *avalancheCarDetail = [CarDetail new];
avalancheCarDetail.carModel = @"Avalanche";
avalancheCarDetail.carMSRP = @"$35,980";
avalancheCarDetail.carMPG = @"21 MPG HWY";
avalancheCarDetail.carAirbag = @"6 Airbags";
avalancheCarDetail.carVolume = @"n/a";
avalancheCarDetail.carHorsepower = @"320";
avalancheCarDetail.bigImage = @"big_avalanche";

CarDetail *silveradoCarDetail = [CarDetail new];
silveradoCarDetail.carModel = @"Silverado";
silveradoCarDetail.carMSRP = @"$23,590";
silveradoCarDetail.carMPG = @"23 MPG HWY";
silveradoCarDetail.carAirbag = @"6 Airbags";
silveradoCarDetail.carVolume = @"n/a";
silveradoCarDetail.carHorsepower = @"195 HP";
silveradoCarDetail.bigImage = @"big_silverado";


_carDetails = [NSMutableArray new];



[_carDetails addObject:sparkCarDetail];

[_carDetails addObject:sonicCarDetail];

[_carDetails addObject:cruzeCarDetail];

[_carDetails addObject:voltCarDetail];

[_carDetails addObject:malibuCarDetail];

[_carDetails addObject:impalaCarDetail];

[_carDetails addObject:ssCarDetail];

[_carDetails addObject:camaroCarDetail];

[_carDetails addObject:corvetteCarDetail];

[_carDetails addObject:equinoxCarDetail];

[_carDetails addObject:traverseCarDetail];

[_carDetails addObject:tahoeCarDetail];

[_carDetails addObject:suburbanCarDetail];

[_carDetails addObject:avalancheCarDetail];

[_carDetails addObject:silveradoCarDetail];





}

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



- (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 [_carModels count];
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath         *)indexPath
    {
        static NSString* kCellIdentifier = @"carTableCell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

        if (cell == nil)
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];

        cell.textLabel.text =  [_carModels objectAtIndex: indexPath.row];
        cell.imageView.image = [UIImage imageNamed:[_carImages objectAtIndex: indexPath.row]];

        return cell;
    }

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

-(void) prepareForSegue: (UIStoryboardSegue *)segue sender: (id)sender
{
    if ([[segue identifier] isEqualToString:@"ShowCarDetails"])
    {
        Chevy2DetailViewController *detailViewController = [segue destinationViewController];

        NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
        int row = [myIndexPath row];

        detailViewController.CarDetailModel = @[_carModels[row], _carImages[row]];
    }
}

@end

谢谢!

4

2 回答 2

0

使用 UITablViews 时,请遵循 cellForRowAtIndexPath 函数的以下标准:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* kCellIdentifier = @"YourCellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];

    cell.textLabel.text =  [_carModels objectAtIndex: indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[_carImages objectAtIndex: indexPath.row]];

    return cell;
}

我不知道这是否也可以解决您的问题,但值得一试。

于 2013-07-17T13:30:47.760 回答
0

所以你的问题在于prepareForSegue你使用的方法。这实际上并不是将数据从一个视图控制器传递到下一个视图控制器。

在你的prepareForSegueMethod(),尝试改变这个:

detailViewController.carDetailModel = @[_carModels[row], _carImages[row]];

对此:

detailViewController.carDetailModel = @[_carDetails[row]];

这应该只是将所选汽车的数组从tableViewController传递到DetailViewController。然后在你的DetailViewController.h文件中,创建一个像这样的属性

@property (nonatomic, strong) NSArray *carDetailModel;

然后在DetailViewController.m里面放一个NSLog这样的:

NSLog(@"contents of the array being passed over here %@", _carDetailModel)

看看它给了你什么。如果您在日志中看到数组的每个部分,那么您只需像这样设置标签,例如:

modelLabel.text = [_carDetailModel valueforKey:@"carModel"];

于 2013-07-17T15:42:19.167 回答