0

これが私がする必要があることです:

66pxx66pxの画像を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 の init に読み込むことができます。

それ以外の場合、セルごとに画像が異なる場合は、それを 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 の混乱よりも良いアプローチは、画像を正しい順序で NSMutableArray にプッシュしてから、単に使用することです

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

于 2010-03-26T20:48:31.120 に答える