有没有办法调整表格视图的大小并获取要包装在iPhone中的文本?我正在使用以下代码,它成功调整单元格的高度,但文本没有换行,它只是以“......”结尾。
- (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;
}
答案 0 :(得分:1)
这里有两件事:
在显示的标签中包装文本 - 要做到这一点,你必须为你的UILabel定义它看起来像一个容器 - 它可以有多少行,它是否包装,当然还有多大。您可以在代码中或在Interface Builder中执行此操作(取决于您是否使用自己的自定义单元格)
您可以在Interface Builder或代码中更改UITableView的宽度(myTableView.frame = CGRectMake(x,y,width,height)) 但请注意,如果您使用UITableViewController作为视图控制器 - 您无法调整表格视图的宽度
答案 1 :(得分:1)
找到了完美的答案
How do I wrap text in a UITableViewCell without a custom cell
这是代码
- (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;
}
答案 2 :(得分:0)
在UILabel中分配您的字符串,并使用此Thread来执行此操作。另外明智的方法是将字符串分配给UItextView并隐藏它。您可以使用
textview.contentSize.height//use this to your cell hieght