问题
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:
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:
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?
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:
How do I get the tableview height adjusted for the row with the 1 line description label is slightly shorter?
Thanks you for your help!
回答1:
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.
now set Header label height Fix and description label make multiline ( Lines = 0)
for table view set in viewDidLoad
_tableView.estimatedRowHeight = 100.0;
_tableView.rowHeight = UITableViewAutomaticDimension;
Let me know if this works...
来源:https://stackoverflow.com/questions/34959491/custom-tableview-cell-with-multiline-uilabel-needs-dynamic-height