具有多线UILabel的自定义Tableview单元需要动态高度 [英] Custom Tableview cell with multiline UILabel needs dynamic height
问题描述
我使用Interface Builder创建了一个自定义TableView单元格。这就是它的样子:
I created a custom TableView cell with Interface Builder. This is what it looks like:
描述标签,我需要自动换行,所以我这样设置:
For the Description Label, I need that to word wrap so I set it as such:
在我的SettingsPageViewController中,我有以下表格视图方法被覆盖:
In my SettingsPageViewController, I have the following Table View methods overridden:
@implementation SBSettingsViewController
{
NSArray *settingHeaders;
NSArray *settingDescriptions;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setupLeftMenuButton];
// Do any additional setup after loading the view from its nib.
settingHeaders = [NSArray arrayWithObjects:@"Label Header 1",@"Label Header 2",nil];
settingDescriptions = [NSArray arrayWithObjects:@"Two line description Two line description Two line description ",@"One Line Description",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [settingHeaders count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"SettingsTableCell";
SettingsTableViewCell *cell = (SettingsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SettingsTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.settingsHeaderLabel.text = [settingHeaders objectAtIndex:indexPath.row];
cell.settingsDescriptionLabel.text = [settingDescriptions objectAtIndex:indexPath.row];
cell.settingsDescriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.settingsDescriptionLabel.numberOfLines = 0;
CGRect appFrame=[[UIScreen mainScreen] bounds];
cell.settingsDescriptionLabel.preferredMaxLayoutWidth = appFrame.size.width - 15;
[cell.settingsDescriptionLabel sizeToFit];
[cell.settingsDescriptionLabel setNeedsDisplay];
[cell layoutIfNeeded];
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 85;
}
以下是结果页面的样子:
Here is what the resulting page looks like:
如您所见,第一个tableview单元格的描述标签不是自动换行。它只是切断了。 如何进行换行?
As you can see, the description label for the 1st tableview cell does NOT word wrap. It just cuts off. How do I make it wrap?
另外,我想要动态调整Tableview单元格高度。我试图将heightForRowAtIndexPath:改为UITableViewAutomaticDimension,但这让它看起来非常奇怪:
Also, I'd like for the Tableview cell height to be dynamically adjusted. I tried to change the heightForRowAtIndexPath: to be UITableViewAutomaticDimension but that just made it look super weird as such:
如何调整行视图高度,1行说明标签略短?
感谢您的帮助!
推荐答案
将标题标题限制为top,leading,trailing to super view和底部(垂直)到描述标签。
对于描述标签>超前视图的前导,尾随和下边距相同。
give constraint of header label top, leading, trailing to super view and bottom (vertical ) to description Label. same for description label > leading, trailing and bottom margin to super view.
现在设置标题标签高度修复和描述标签make multiline(Lines = 0)
now set Header label height Fix and description label make multiline ( Lines = 0)
在viewDidLoad中设置的表格视图
for table view set in viewDidLoad
_tableView.estimatedRowHeight = 100.0;
_tableView.rowHeight = UITableViewAutomaticDimension;
如果有效,请告诉我......
Let me know if this works...
这篇关于具有多线UILabel的自定义Tableview单元需要动态高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!