繁体   English   中英

如何将图像加载到自定义UITableViewCell?

[英]How to load images into Custom UITableViewCell?

这是我需要做的:

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

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

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

是的,不推荐使用cell.image。 在默认的TableViewCell中使用imageview.image代替。 我不确定为什么需要自定义单元格才能执行标准tableviewcell已经执行的操作(标题,字幕和使用UITableViewStyleSubtitle的图像)

更好的方法是将图像按正确的顺序推送到NSMutableArray上,然后使用

cell.image = [myImages objectAtIndex:indexPath.row];

现在可以使用了。 Seventoes,关于将其放入tableView:cellForRowAtIndexPath: ,您是正确的tableView:cellForRowAtIndexPath:

indexPath.row是我所缺少的。 工作结果如下:

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        MyCustomCell *cell = (MyCustomCell*)[tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

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

        }

if (indexPath.row == 1)
{
cell.image = [UIImage imageNamed:@"foo.png"];
}
else if (indexPath.row == 2)
cell.image = [UIImage imageNamed:@"bar.png"];
}
...
else
{
cell.image = [UIImage imageNamed:@"lorem.png"];
}

return.cell;
}

暂无
暂无

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

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