问题
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;
}
回答1:
There are two things here:
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)
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
回答2:
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;
}
回答3:
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
来源:https://stackoverflow.com/questions/5101556/iphone-adjust-size-of-table-view-according-to-text