2009-04-03 12 views
1

Dieses Problem ist einfach, aber entscheidend und dringend. Hier ist, was getan werden muss:Wie Bilder in benutzerdefinierte UITableViewCell geladen werden?

laden 66px x 66px Bilder in die Tabellenzellen in der MainViewController-Tabelle. jeder TableCell hat ein einzigartiges Bild.

Aber wie?

Würden wir cell.image verwenden? ...

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

Wenn ja, wo?

Ist eine if/else-Anweisung erforderlich? Hilfe?

Hier ist der Projektcode, gehostet auf Google Code, für einfache und schnelle Hilfe ... http://www.google.com/codesearch/p?hl=en#Fcn2OtVUXnY/trunk/apple-sample-code/NavBar/NavBar/MyCustomCell.m&q=MyCustomCell%20lang:objectivec

jeder Zelle Etiketten zu laden, Mainviewcontroller ein NSDictionary und NSLocalizedString wie so verwendet ...

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

HILFE?

+0

Der verknüpfte Code ist weg. –

Antwort

6

Wenn das Bild für jede Zelle gleich ist, d. H. Es ist Teil dieses Zelltyps, können Sie es in die Init von MyCustomCell laden, indem Sie self.image = [UIImage imageNamed: "blabla"];

Andernfalls, wenn das Bild für verschiedene Zellen anders sein wird, wäre es logischer, in einer Tabellen- zu setzen: cellForRowAtIndexPath:

1

Es funktioniert jetzt. Du hattest Recht, Seventoes, über das Einsetzen in tableView:cellForRowAtIndexPath:

indexPath.row war was ich fehlte. Das Arbeitsergebnis geht so:

 - (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; 
} 
+4

Wird cell.image nicht abgeschrieben? –

+1

UIImage * cellIcon = [UIImake imageNamed: @ "Chart.png"]; [[Zelle imageView] setImage: cellIcon]; das wird nicht beraubt – Pooja

1

Ein besserer Ansatz dann, wenn-sonst Chaos Ihre Bilder auf eine NSMutableArray in der richtigen Reihenfolge sein würde, schieben, und verwenden Sie dann nur

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

3

Ja, cell.image ist veraltet. Verwenden Sie stattdessen imageview.image in der Standard-TableViewCell. Ich bin mir nicht sicher, warum benutzerdefinierte Zelle erforderlich war, was die Standardtableviewcell schon tut (Titel, Untertitel und ein Bild mit UITableViewStyleSubtitle)

Verwandte Themen