0

这是我需要做的:

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

4 回答 4

6

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

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

于 2009-04-03T19:18:38.513 回答
3

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

于 2011-02-08T20:27:38.050 回答
1

现在可以了。你是对的,Seventoes,关于把它放进去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;
}
于 2009-04-03T22:44:16.147 回答
1

一个更好的方法是 if-else mess 是以正确的顺序将图像推送到 NSMutableArray 上,然后使用

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

于 2010-03-26T20:48:31.120 回答