2009-12-10 15 views
0

Je veux afficher dans un UITableViewCell, texte commeComment est-ce que je peux faire le 'caractère de retour' pour un texte avec plusieurs lignes dans une étiquette de xml, que j'analyser puis mettre dans un UItableView?

line1 line2

je l'obtenir à partir d'une balise xml comme ligne1 ligne2

J'ai essayé beaucoup de choses, comme: <br/> (also &lt;br&gt;),
\n which only displays "line1 \n line2",
<![CDATA[<br >]]> which only displays "line1 <br> line2".

Merci pour l'aide

Répondre

1

êtes-vous la définition du texte comme: cell.textLabel.text = myText?

Dans ce cas, vous l'envoyez à UILabel, et UILabels ne peut pas avoir de saut de ligne. Ce que vous pouvez essayer est de créer une cellule personnalisée avec un UITextView et envoyer votre texte là-bas.

Exemple d'une cellule personnalisée: La classe tableview:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"quickListViewCell"; 
    quickListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil){ 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"quickListViewCell" owner:nil options:nil]; 

     for(id currentObject in topLevelObjects) 
     { 
      if([currentObject isKindOfClass:[quickListViewCell class]]) 
      { 
       cell = (quickListViewCell *)currentObject; 
       break; 
      } 
     } 
    } 

    [[cell textFieldText] setText:@"Line 1 \n Line 2"]; 

    return cell; 
} 

quickListViewCell.h

#import <UIKit/UIKit.h> 

@interface appCountryCategoryViewCell : UITableViewCell { 
    IBOutlet UITextView *textFieldText; 
} 

@property (nonatomic, retain) IBOutlet UITextView *textFieldText; 

@end 

quickListViewCell.m

#import "quickListViewCell.h" 

@implementation quickListViewCell 

@synthesize textFieldText; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 
     // Initialization code 
    } 
    return self; 
} 


- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 

    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 


- (void)dealloc { 
    [super dealloc]; 
} 


@end 

Créer un UITableViewCell dans IB et définir la identifiant à "quickListViewCell".

+0

Paul, merci pour la réponse. Je suis d'accord avec votre exemple, mais j'essaie d'obtenir le texte d'une balise xml, et même si j'écris line1 \ n line2 dans le xml, le texte affiché est seulement "line1 \ n line2". – Eric

+0

Avoir essayé de l'écrire comme un saut de ligne et en utilisant un textView. par exemple. ligne 1 Ligne 2 /Paul –

+0

Ok merci beaucoup Paul, ça marche. Alors permettez-moi de résumer. Votre code est correct, sauf quelques erreurs: dans quickListViewCell.h vous avez écrit '@interface appCountryCategoryViewCell' mais il s'agit en fait de '@interface QuickListViewCell'. et j'ai remplacé la ligne '[[cell textFieldText] setText: @ "Ligne 1 \ n Ligne 2"];' par cell.textFieldText.text = [self nameForIndexPath: indexPath] Dans mon fichier XML, je fais un vrai saut de ligne entre les deux mots ligne 1 Ligne 2. Dans un fichier xib appelé QuickListViewCell.xib, j'ai ajouté un QuickListViewCell. J'ai ajouté un UItextView à l'intérieur duquel je me suis connecté avec la quicklistviewcell. – Eric