• Apfeltalk ändert einen Teil seiner Allgemeinen Geschäftsbedingungen (AGB), das Löschen von Useraccounts betreffend.
    Näheres könnt Ihr hier nachlesen: AGB-Änderung

TableView: Erstes und Letztes Object verändern sich

GreenApple

Reinette de Champagne
Registriert
20.05.07
Beiträge
412
Doch ich hab das Dokument gelesen und hab versucht es anzupassen, habe jetzt auch noch einmal verglichen, aber weiß nicht genau was noch fehlt, oder falsch ist.
 

philo

Roter Stettiner
Registriert
13.10.04
Beiträge
973
schau dir mal meinen Post noch mal an und gucke wo genau die Klammern sind!

diese Zeile sehe ich bei dir nicht:

Code:
UILabel* mainLabel = (UILabel*)[cell viewWithTag:1];

Sie holt das Label aus der bereits exisitierende Cell. Die nur einmal in:

Code:
if(cell == nil){..}

erzeugt wurde.
 

GreenApple

Reinette de Champagne
Registriert
20.05.07
Beiträge
412
Hab ich schon mal dabei gehabt, aber dann stürzt das Programm ab.
 

GreenApple

Reinette de Champagne
Registriert
20.05.07
Beiträge
412
#define MAINLABEL_TAG 1
#define DETAILLABEL_TAG 2

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

CheapEatAppDelegate *appDelegate = (CheapEatAppDelegate *)[[UIApplication sharedApplication] delegate];
Rezept *r = (Rezept *)[appDelegate.rezepte objectAtIndex:indexPath.row];


if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];

cell.imageView.image = [UIImage imageNamed:[r imageSmall]];
cell.selectionStyle = UITableViewCellSelectionStyleGray;

UILabel *mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(62, 13, 220, 19)] autorelease];
mainLabel.text = r.name;
mainLabel.font = [UIFont fontWithName:mad:"Helvetica Neue" size:19.0];
mainLabel.textColor = [UIColor colorWithRed:255.0/255.0 green:216.0/255.0 blue:0.0/255.0 alpha:1.0/1.0];
mainLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
mainLabel.backgroundColor = [UIColor clearColor];
mainLabel.tag = MAINLABEL_TAG;

UILabel *detailLabel = [[[UILabel alloc] initWithFrame:CGRectMake(59, 35, 230, 14)] autorelease];
detailLabel.textAlignment = UITextAlignmentLeft;
detailLabel.font = [UIFont fontWithName:mad:"Helvetica Neue" size:11.0];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor lightGrayColor];
detailLabel.text = [r info];
detailLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
detailLabel.tag = DETAILLABEL_TAG;

[cell.contentView addSubview:detailLabel];
[cell.contentView addSubview:mainLabel];

} else {
UILabel *mainLabel = (UILabel *)[cell viewWithTag:MAINLABEL_TAG];
UILabel *detailLabel = (UILabel *)[cell viewWithTag:DETAILLABEL_TAG];
}

NSDictionary *aDict = [appDelegate.rezepte objectAtIndex:indexPath.row];

mainLabel.text = [aDict objectForKey:mad:"mainTitleKey"];
detailLabel.text = [aDict objectForKey:mad:"detailTitleKey"];

return cell;

}

Ich hab's jetzt nochmal wie im dem geposteten Dokument gemacht, aber das Programm stürzt ab, ich weiß echt nicht was ich falsch mache.
 

philo

Roter Stettiner
Registriert
13.10.04
Beiträge
973
Code:
UILabel *mainLabel

steht zwei mal da und damit definierst du lokal neu.

EDIT : und auch UILabel *detailLabel
 

philo

Roter Stettiner
Registriert
13.10.04
Beiträge
973
so muss es doch aussehen:
Code:
UILabel *mainLabel
...
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
  ...
     mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 15.0)]          autorelease];
   ...
}
else {
      mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
 }
mainLabel.text = [aDict objectForKey:@"mainTitleKey"];
 
Zuletzt bearbeitet:
  • Like
Reaktionen: GreenApple

GreenApple

Reinette de Champagne
Registriert
20.05.07
Beiträge
412
Dank Leute für eure Tipps ich hab's jetzt!!!

Hier der nun richtige Code:

#define MAINLABEL_TAG 1
#define DETAILLABEL_TAG 2
#define PHOTO_TAG 3

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

CheapEatAppDelegate *appDelegate = (CheapEatAppDelegate *)[[UIApplication sharedApplication] delegate];
Rezept *r = (Rezept *)[appDelegate.rezepte objectAtIndex:indexPath.row];

UILabel *mainLabel;
UILabel *detailLabel;
UIImageView *photo;

if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];

cell.selectionStyle = UITableViewCellSelectionStyleGray;

mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(62, 13, 220, 21)] autorelease];
mainLabel.font = [UIFont fontWithName:mad:"Helvetica Neue" size:19.0];
mainLabel.textColor = [UIColor colorWithRed:255.0/255.0 green:216.0/255.0 blue:0.0/255.0 alpha:1.0/1.0];
mainLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
mainLabel.backgroundColor = [UIColor clearColor];
mainLabel.tag = MAINLABEL_TAG;

detailLabel = [[[UILabel alloc] initWithFrame:CGRectMake(59, 35, 230, 14)] autorelease];
detailLabel.textAlignment = UITextAlignmentLeft;
detailLabel.font = [UIFont fontWithName:mad:"Helvetica Neue" size:11.0];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor lightGrayColor];
detailLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
detailLabel.tag = DETAILLABEL_TAG;

photo = [[[UIImageView alloc] initWithFrame:CGRectMake(5.0, 5.0, 50.0, 50.0)] autorelease];
photo.tag = PHOTO_TAG;

[cell.contentView addSubview:detailLabel];
[cell.contentView addSubview:mainLabel];
[cell.contentView addSubview:photo];

} else {
mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
detailLabel = (UILabel *)[cell.contentView viewWithTag:DETAILLABEL_TAG];
photo = (UIImageView *)[cell.contentView viewWithTag:pHOTO_TAG];
}

mainLabel.text = [r name];
detailLabel.text = [r info];

UIImage *theImage = [UIImage imageNamed:[r imageSmall]];
photo.image = theImage;

return cell;

}

Das Dictionary hat mich voll verwirrt!
 

Poljpocket

Salvatico di Campascio
Registriert
07.01.07
Beiträge
432
für ein ander Mal: Code bitte einrücken, ich will mir das so nicht ansehen... :(
 

GreenApple

Reinette de Champagne
Registriert
20.05.07
Beiträge
412
in XCode ist der Code ja eingerückt, aber ich will jetzt nicht alle Zeilen jedes mal durchgehen.