So laden Sie Bilder in Custom UITableViewCell?
Hier ist, was ich tun muss:
Laden 66px x 66px Bilder in die Zellen der Tabelle in die MainViewController Tabelle.
jeder TableCell hat ein einzigartiges Bild.
Aber wie? Würden wir cell.image
?
cell.image = [UIImage imageNamed:@"image.png"];
Wenn ja, wo? Ist eine if/else-Anweisung erforderlich?
Laden jede Zelle, labels, MainViewController verwendet ein NSDictionary und NSLocalizedString etwa so:
//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;
}
- Der verlinkte code ist Weg.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Wenn das Bild die gleiche für jede Zelle, D. H., es ist Teil dieser Art von Zelle, könnten Sie laden Sie es in MyCustomCell init, mit selbst.image = [UIImage imageNamed:"blabla"];
Ansonsten, wenn das Bild wird für verschiedene Zellen, wäre es logischer, es in tableView:cellForRowAtIndexPath:
Ja, Zelle.Bild ist veraltet. verwenden Bildansicht.Bild statt in der Standard-TableViewCell. Ich bin nicht sicher, warum benutzerdefinierte Zelle erforderlich war, zu tun, was die standard-tableviewcell schon funktioniert (Titel, Untertitel, und ein Bild mit UITableViewStyleSubtitle)
Funktioniert es jetzt. Sie hatten Recht, Seventoes, etwa indem es in
tableView:cellForRowAtIndexPath:
indexPath.row
war, was ich war, war mir fehlt. Die arbeiten Ergebnis geht so:Bessere Ansatz ist, dann, dass if-else-Sauerei wäre, schieben Sie Ihre Bilder auf ein NSMutableArray in der richtigen Reihenfolge, und verwenden Sie dann
Zelle.Bild = [myImages objectAtIndex:indexPath.Zeile];