1

Is there a way to adjust the size of a table view AND get the text to wrap in iPhone? I'm using the following code, and it succeeds in adjusting the height of the cell, but the text doesn't wrap, it just ends with "...."

- (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath    {
    CGSize cellHeight;

        City *thisCity = [cities objectAtIndex:indexPath.row];
        NSString * myString = thisCity.cityName;

        cellHeight = [myString sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(300.0, 1000.0) lineBreakMode:UILineBreakModeWordWrap];
        return cellHeight.height + 20;
}
Jack BeNimble
  • 35,733
  • 41
  • 130
  • 213

3 Answers3

1

There are two things here:

  1. Wrapping of text in the displayed label - to do this you have to define for your UILabel how it looks as a container - how many rows can it have, does it wrap and of course how big it is. You can do this either in code or in the Interface Builder (depending on if you are using your own custom cell)

  2. You can change your UITableView's width either in the Interface Builder or in code (myTableView.frame = CGRectMake(x,y,width,height)) BUT notice the if you are using a UITableViewController as your view controller - you CANNOT adjust the width of the table view

shein
  • 1,834
  • 15
  • 23
1

Found the perfect answer at

How do I wrap text in a UITableViewCell without a custom cell

Here's the code

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


    UITableViewCell *cell =
    [ tv dequeueReusableCellWithIdentifier:@"cell"];
    if( nil == cell ) {

        // this sets up a resusable table cell 
        cell = [ [[UITableViewCell alloc]
                  initWithFrame:CGRectZero reuseIdentifier:@"cell"] autorelease];

        // support line break mode
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

        // 0 means any number of lines
        cell.textLabel.numberOfLines = 0;

        // set it up with a consistent font with the height calculation (see below)
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];    

    }


    // set the name of the city 

    if (indexPath.row < cities.count ) {
        City *thisCity = [cities objectAtIndex:indexPath.row];
        cell.textLabel.text = thisCity.cityName;
        } 
        else 
        {

        cell.textLabel.text = @"Add New City...";
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


    return cell;

}

// get the height of the row

- (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath    {

    City *thisCity = [cities objectAtIndex:indexPath.row];
    NSString * cellText = thisCity.cityName;

    // set a font size
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];

    // get a constraint size - not sure how it works 
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);

    // calculate a label size - takes parameters including the font, a constraint and a specification for line mode
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    // give it a little extra height
    return labelSize.height + 20;

}
Community
  • 1
  • 1
Jack BeNimble
  • 35,733
  • 41
  • 130
  • 213
0

Assign your String in a UILabel.and use this Thread to do. other wise Assign the string to a UItextView and hidden it.you can use

 textview.contentSize.height//use this to your cell hieght
Community
  • 1
  • 1
Sat
  • 1,616
  • 3
  • 22
  • 40