views:

44

answers:

1

I want to display in a UItableViewCell, text like

line1 line2

I get it from an xml tag like line1 line2

I've tried a lot of things, like: <br/> (also &lt;br&gt;),
\n which only displays "line1 \n line2",
<![CDATA[<br >]]> which only displays "line1 <br> line2".

Thanks for the help

+1  A: 

are you setting the text like: cell.textLabel.text = myText?

I that case you are sending it to an UILabel, and UILabels can't have linebreaks. What you can try is to create a custom cell with an UITextView and send your text there.

Example of a custom cell: The tableview class:

- (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

Create a UITableViewCell in IB and set the identifier to "quickListViewCell".

Hope my answer helped you.

Best regards, Paul Peelen

Paul Peelen
Paul, thanks for the answer.I agree with your example, but I am trying to get the text from an xml tag, and even if I write <tag>line1 \n line2</tag> in the xml, the text diplayed is only "line1 \n line2".
Eric
Have to tried writing it as an actual line break and using a textView.e.g. <tag>line 1 Line 2</tag>/Paul
Paul Peelen
Ok thanks so much Paul, it does work.So let me just summarize.Your code is right, except some mistakes:in quickListViewCell.h you wrote '@interface appCountryCategoryViewCell' but it is in fact '@interface QuickListViewCell'.and I replaced the line '[[cell textFieldText] setText:@"Line 1 \n Line 2"];' by cell.textFieldText.text = [self nameForIndexPath:indexPath]In my xml file I do a real line break between the two words <tag>line 1 Line 2</tag>.In a xib called QuickListViewCell.xib, I added a QuickListViewCell. I added a UItextView inside which I linked with the quicklistviewcell.
Eric