我有一个带有 uitableviewcells 的 uitableview,但是我的一些文本太长了,它一直延伸到单元格的边缘,我的附件勾选 View 将在选择时出现。
如果文本超出宽度并进入第二行,我目前正在换行。但是我希望有一个很好的方法来限制这个 UITableViewCell 标签的宽度。
最佳答案
在您的 cellForRowAtIndexPath: 函数中。第一次创建单元格时:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
}
现在指定 UITableViewCell 的大小,在 heightForRowAtIndexPath 函数中执行此操作:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *str = [[arrTexts objectAtIndex:indexPath.row]; // filling text in label
CGSize maximumSize = CGSizeMake(300, 100); // change width and height to your requirement
CGSize strSize = [str sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17] constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap] //dynamic height of string depending on given width to fit
return (10+strSize.height+10) // caculate on your bases as u have string height
}
关于iphone - 设置 UITableViewCell 文本的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12085174/