Reputation: 63
I have a scenario in which a single UITableViewCell
have 2 UILable
. I can have long text in it coming from server which causes both labels to be multiline at same time.
I have implemented autolayout and
label.numberofline = 0;
tableVieww.estimatedRowHeight = 100.0;
tableVieww.rowHeight = UITableViewAutomaticDimension;
but it results only one label to expand not both. Please help.
Thanks in advance
Upvotes: 2
Views: 1176
Reputation: 24205
What You have so far looks fine. That should work without the need calculate the label height as some suggested.
Just make sure that you have the proper constraints on your labels.
I tested it. it's working!
- (void)viewDidLoad {
[super viewDidLoad];
dynamicTableVuew.rowHeight = UITableViewAutomaticDimension;
dynamicTableVuew.estimatedRowHeight = 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId"];
UILabel *label1 = (UILabel *)[cell viewWithTag:1];
UILabel *label2 = (UILabel *)[cell viewWithTag:2];
label1.text = @"Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text";
label2.text = @"Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text Very long text";
return cell;
}
That's all I have in the view controller. and nothing special in storyboard.
One last thing to check. Make sure that your tableview is connected to your view controller with an outlet. You may see the outlet code there but does not mean its connected. maybe it was removed by mistake. the following lines will not take effect:
tableVieww.estimatedRowHeight = 100.0;
tableVieww.rowHeight = UITableViewAutomaticDimension;
Upvotes: 0
Reputation: 980
try this:
use this code to calculate row height
Note : ExtraHeight"H"
Suppose you have cell with height 100 and in that cell a label has height 20 than extra height will be 80, because this code is calculating the height of label as per text with its size.
you are going to put this code in heightforRowatindexpath so cell.label can't get there so just pass the text and use Method-2
Method - 1:
-(CGFloat)setRowHeightWithLabel:(UILabel *)lbl withLabelText:(NSString *)lblText withExtraHeight:(CGFloat)H withFont:(CGFloat)fontSize
{
CGFloat totalHeight;
CGFloat lblWidth = lbl.frame.size.width;
CGSize lblTextSize = [lblText boundingRectWithSize:CGSizeMake(lblWidth, MAXFLOAT)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;
if (lblTextSize.height > lbl.frame.size.height) {
totalHeight = (lblTextSize.height+H);
}
else
{
totalHeight = lbl.frame.size.height+H;
}
NSLog(@"+++++++++++++Height - %.2f",totalHeight);
return totalHeight;
}
Use this for use in heightforRow method
Method - 2:
-(CGFloat)setRowHeightWithLabelText:(NSString *)lblText withExtraHeight:(CGFloat)H withFont:(CGFloat)fontSize
{
CGFloat totalHeight;
CGFloat lblWidth = 300;
CGSize lblTextSize = [lblText boundingRectWithSize:CGSizeMake(lblWidth, MAXFLOAT)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;
if (lblTextSize.height > 20) {
totalHeight = (lblTextSize.height+H);
}
else
{
totalHeight = 20+H;
}
NSLog(@"+++++++++++++Height - %.2f",totalHeight);
return totalHeight;
}
Upvotes: 3
Reputation: 685
I think you are using autolayout I had the same problem I resolved it by setting maximum width of labels and the in label's size Inspector you should set preferred width to your max width
Upvotes: 1
Reputation: 3393
If you want two labels to be multi-line (supposing you are using "Subtitle" cell type"), you must set numberOfLines to 0 to both labels, eg:
UITableViewCell cell = [UITableViewCell alloc]init];
cell.textLabel.numberOfLines = 0;
cell.detailTextLabel.numberOfLines = 0;
Your lines about cell height seems to be correct
Upvotes: -2