Craig Craig - 1 year ago 87
iOS Question

Making all the text show in a UITableView Cell

I am loading an array into a UITableView and the components of the array are long. What I want to do is customize the cells so that thier lenght will adjust to fit the text but the width will stay the same. Right now what is happening is when the text gets too long it will just write off the cell and you will see this.

enter image description here

I would like to see the text hit the end of the cell and then start a new line.

My code right now looks like this.

@implementation CRHCommentSection
@synthesize observationTable;

NSArray *myArray;

- (void)viewDidLoad

myArray = [CRHViewControllerScript theArray];
NSLog(@"%@", myArray);
//NSArray* paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:1]];
//[observationTable insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];

[observationTable reloadData];

[super viewDidLoad];
// Do any additional setup after loading the view.


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.

NSLog(@" in method 1");
return 1;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

NSLog(@" in method 2");
// Return the number of rows in the section.
return [myArray count];


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

NSLog(@" in method 3");

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier];

cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
//cell.textLabel.text = @"Label";

return cell;

I also found some code that somebody else wrote for multiple lined cells but don't know how to make it automatic based on the length of my string from the array.

static NSString *CellIdentifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = @"Label';
cell.detailTextLabel.text = @"Multi-Line\nText";
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;

They said that you will also need to return a suitable height for the multi-line cell and that a height of (44.0 + (numberOfLines - 1) * 19.0) should work fine.

Does anyone have any ideas of how to do this?


Answer Source

You will need to work with following methods

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    // this method is called for each cell and returns height
    NSString * text = @"Your very long text";
    CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize: 14.0] forWidth:[tableView frame].size.width - 40.0 lineBreakMode:NSLineBreakByWordWrapping];
    // return either default height or height to fit the text
    return textSize.height < 44.0 ? 44.0 : textSize.height;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString * cellIdentifier = @"YourTableCell";

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 

        [[cell textLabel] setNumberOfLines:0]; // unlimited number of lines
        [[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
        [[cell textLabel] setFont:[UIFont systemFontOfSize: 14.0]];

    [[cell textLabel] setText:@"Your very long text"];

    return cell;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download