无法在表格单元格中查看图像

时间:2013-03-20 09:44:37

标签: iphone ios objective-c xcode uitableview

我正在做this教程,但在编写代码后,我无法在表格单元格中看到任何图像 - >他们是白人!

我在Supporting Files文件夹中添加了图片文件,再次阅读教程,但无法解决这个问题!拜托,有人可以帮我吗?

@interface CarTableViewController ()

@end

@implementation CarTableViewController

@synthesize  carMakes = _carMakes;
@synthesize carImages = _carImages;
@synthesize carModels = _carModels;

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.carMakes = [[NSArray alloc] initWithObjects:@"Chevy", @"BMW", @"Toyota", @"Volvo", @"Smart", nil];
    self.carModels = [[NSArray alloc] initWithObjects:@"Volt", @"Mini", @"Venza", @"S60", @"Fortwo", nil];
    self.carImages = [[NSArray alloc] initWithObjects:@"chevy_volt.jpg", @"mini_clubman.jpg", @"toyota_venza.jpg", @"volvo_s60.jpg", @"smart_fortwo.jpg", nil];
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.carModels count];
}

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

    CarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CarTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                        reuseIdentifier:CellIdentifier];
    }
        cell.makeLabel.text = [self.carMakes objectAtIndex:[indexPath row]];
        cell.modelLabel.text = [self.carModels objectAtIndex:[indexPath row]];
        UIImage *carPhoto = [UIImage imageNamed:[self.carImages objectAtIndex: [indexPath row]]];
        cell.carImage.image = carPhoto;

    return cell;
}

2 个答案:

答案 0 :(得分:0)

试试这个::

UIImage *im = [UIImage imageNamed:[NSString stringWithFormat:@"%@", self.carImages objectAtIndex: [indexPath row]]];
cell.imgCar.image = im;

如果仍无效,则表示您的图片未正确复制到项目中。请删除并重新添加。

并且不要伪造到 @synthesize 你的imageView

谢谢,Happy Coding ..

答案 1 :(得分:0)

尝试在xib中设置方面适合度或遵循代码

cell.imgCar.image.imageViewContentmode = UIViewContentModeAspectFit;