2011-01-20 4 views

Répondre

3

Il y a deux options (peut-être plus). Vous pouvez utiliser les propriétés UITableViewCell natives pour ajouter du contenu à la cellule ou créer une cellule personnalisée (je veux dire par là ajouter vos propres sous-vues à la cellule). Pour commencer, essayez le premier, c'est simple et élégant et les résultats seront plutôt bons. Par exemple, essayez la méthode de création de cellules suivantes:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     // notice the Style. The UITableViewCell has a few very good styles that make your cells look very good with little effort 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell... 
    // In my case I get the data from the elements array that has a bunch on dictionaries 
    NSDictionary *d = [elements objectAtIndex:indexPath.row]; 

    // the textLabel is the main label 
    cell.textLabel.text = [d objectForKey:@"title"]; 

    // the detailTextLabel is the subtitle 
    cell.detailTextLabel.text = [d objectForKey:@"date"]; 

    // Set the image on the cell. In this case I load an image from the bundle 
    cell.imageView.image = [UIImage imageNamed:@"fsaint.png"]; 

    return cell; 
} 
0

Je suis un grand fan de passer outre la classe UITableViewCell, et de faire le dessin personnalisé dans self.contentView. Cette technique est un peu plus compliquée, mais elle conduit à de bien meilleures performances de défilement.

Par exemple, disons que vous remplacez votre cellule, et ont 3 propriétés sur elle comme ceci:

@property(nonatomic, retain) UIImage *userPic; 
@property(nonatomic, retain) NSString *label; 
@property(nonatomic, retain) NSString *date; 

Ensuite, vous pouvez les dessiner dans la cellule en utilisant la (drawRect :) Fonction:

- (void)drawRect:(CGRect)rect { 
    [super drawRect:rect]; 
    [userPic drawInRect: CGRectMake(10, 5, 50, 50)]; 
    [label drawAtPoint:CGPointMake(70, 5) withFont:[UIFont boldSystemFontOfSize:17]]; 
    [date drawAtPoint:CGPointMake(70, 30) withFont:[UIFont systemFontOfSize:14]]; 
    } 

Pour plus d'exemples, essayez ce cadre qui utilise ce style: https://github.com/andrewzimmer906/XCell

Questions connexes