How can I make the 'return character' for a text with several lines in an xml tag, which I parse then put in a UItableView?

StackOverflow https://stackoverflow.com/questions/1880402

Question

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

Was it helpful?

Solution

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".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top