当前位置:  开发笔记 > 编程语言 > 正文

如何将图像加载到Custom UITableViewCell?

如何解决《如何将图像加载到CustomUITableViewCell?》经验,为你挑选了1个好方法。

这是我需要做的事情:

将66px x 66px图像加载到MainViewController表中的表格单元格中.每个TableCell都有一个独特的图像.

但是怎么样?我们会用cell.image吗?

cell.image = [UIImage imageNamed:@"image.png"];

如果是的话,在哪里?是否需要if/else语句?

要加载每个单元格的标签,MainViewController使用NSDictionary和NSLocalizedString,如下所示:

 //cell one
    menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
    NSLocalizedString(@"PageOneTitle", @""), kTitleKey,
    NSLocalizedString(@"PageOneExplain", @""), kExplainKey, nil]];

    //cell two
    menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
    NSLocalizedString(@"PageOneTitle", @""), kTitleKey,
    NSLocalizedString(@"PageOneExplain", @""), kExplainKey, nil]];

...

 // this is where MainViewController loads the cell content
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = (MyCustomCell*)[tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

if (cell == nil)
{
cell = [[[MyCustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:kCellIdentifier] autorelease];

}

...

    // MyCustomCell.m adds the subviews
- (id)initWithFrame:(CGRect)aRect reuseIdentifier:(NSString *)identifier
{
self = [super initWithFrame:aRect reuseIdentifier:identifier];
if (self)
{
// you can do this here specifically or at the table level for all cells
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

// Create label views to contain the various pieces of text that make up the cell.
// Add these as subviews.
nameLabel = [[UILabel alloc] initWithFrame:CGRectZero]; // layoutSubViews will decide the final frame
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.opaque = NO;
nameLabel.textColor = [UIColor blackColor];
nameLabel.highlightedTextColor = [UIColor whiteColor];
nameLabel.font = [UIFont boldSystemFontOfSize:18];
[self.contentView addSubview:nameLabel];

explainLabel = [[UILabel alloc] initWithFrame:CGRectZero]; // layoutSubViews will decide the final frame
explainLabel.backgroundColor = [UIColor clearColor];
explainLabel.opaque = NO;
explainLabel.textColor = [UIColor grayColor];
explainLabel.highlightedTextColor = [UIColor whiteColor];
explainLabel.font = [UIFont systemFontOfSize:14];
[self.contentView addSubview:explainLabel];

  //added to mark where the thumbnail image should go 
  imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 66, 66)];
  [self.contentView addSubview:imageView];
}

return self;
}

Seventoes.. 6

如果图像对于每个单元格都是相同的,即它是该类型单元格的一部分,则可以使用self.image = [UIImage imageNamed:"blabla"]将其加载到MyCustomCell的init中;

否则,如果不同单元格的图像不同,则将它放在tableView:cellForRowAtIndexPath中更合乎逻辑:



1> Seventoes..:

如果图像对于每个单元格都是相同的,即它是该类型单元格的一部分,则可以使用self.image = [UIImage imageNamed:"blabla"]将其加载到MyCustomCell的init中;

否则,如果不同单元格的图像不同,则将它放在tableView:cellForRowAtIndexPath中更合乎逻辑:

推荐阅读
刘美娥94662
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有