简体   繁体   English

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

[英]Can't view an image in table cell

guys 伙计们

I'm doing this tutorial but after writing the code I can't see any images in the table cells -> they're white! 我正在做教程,但是编写代码后,我在表格单元格中看不到任何图像->它们是白色的!

I added the picture files in the Supporting Files folder, read the tutorial one more time, but can't solve this problem! 我在“支持文件”文件夹中添加了图片文件,又读了一次教程,但不能解决此问题! Please, can anyone help me with this? 拜托,有人可以帮我吗?

@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;
}

Try this :: 尝试这个 ::

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

If still not working, then your image is not properly copied in your project. 如果仍然无法正常工作,则说明图像未正确复制到项目中。 Kindly remove and add it again. 请删除并再次添加。

And dont forgert to @synthesize your imageView 并且不要伪造@synthesize您的imageView

Thanks, Happy Coding.. 谢谢,快乐编码..

尝试在Xib中设置方面适合的内容或通过以下代码

cell.imgCar.image.imageViewContentmode = UIViewContentModeAspectFit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM