【问题标题】:Can't view an image in table cell无法在表格单元格中查看图像
【发布时间】:2013-03-20 09:44:36
【问题描述】:

伙计们

我正在做 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;
}

【问题讨论】:

  • 检查名称是否与您在 Supporting Files 文件夹中添加的名称相同
  • 你设置了 imageView IBOutletConnection 吗?
  • 按照教程进行编码没有问题。请确保您将 IBOutletConnections 正确连接到 CustomCell 而不是 FilesOwner
  • 您的代码是 nop... 但请参阅 IBOutlet 连接 .... 不要跟随“Sync”调用,请尝试调用“Asyc”调用.....developer.apple.com/library/ios/#samplecode/LazyTableImages/…
  • @Nims 名字相同

标签: iphone ios objective-c xcode uitableview


【解决方案1】:

试试这个 ::

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

如果仍然无法正常工作,则说明您的图像未正确复制到您的项目中。请删除并重新添加。

别忘了@synthesize你的imageView

谢谢,编码愉快..

【讨论】:

  • dl.dropbox.com/u/15833199/1.png 所以,文件没有被正确复制 -> 但现在又出了问题.. 有什么想法吗?
  • 如果我们在tableView中使用customCell,那么我们必须在customCell的xib中合成它。
【解决方案2】:

尝试设置适合 xib 的方面或通过以下代码

cell.imgCar.image.imageViewContentmode = UIViewContentModeAspectFit;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多