Reputation: 2952
I am Creating One tableview with tableview cell fro XIB. When I integrate tableview cell with Tableview and run it it is not showing perfectly ,It shows me override.
static NSString *cellIdentifier =@"DiscussionTableViewCell";
DiscussionTableViewCell *cell = (DiscussionTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DiscussionTableViewCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
// [cell.btnConnect addTarget:self action:@selector(btnUserConnectClick:) forControlEvents:UIControlEventTouchUpInside];
// [cell.btnNotnow addTarget:self action:@selector(btnNotNowClick:) forControlEvents:UIControlEventTouchUpInside];
self.cellHeight += cell.frame.size.height;
NSLog(@"Cell height is %f",self.cellHeight);
Upvotes: 1
Views: 191
Reputation: 181
The code I have used to calculate cell content height dynamically is:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_arrMainTable.count && (indexPath.row < _arrMainTable.count))
{
NSDictionary *dict = [_arrMainTable objectAtIndex:indexPath.row];
return [self expectedLabelHeight:dict];
}
return 0;
}
- (CGFloat)expectedLabelHeight:(NSDictionary *)dict
{
CGSize boundingSize = CGSizeMake(CGRectGetWidth(_faqTableView.bounds)-20.0, CGFLOAT_MAX);
UIFont *font = [UIFont fontWithName:@"Roboto-Regular" size:17];
CGRect qstnRect = [dict[@"question"] boundingRectWithSize:boundingSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes: @{NSFontAttributeName:font} context:nil];
font = [UIFont fontWithName:@"Roboto-Light" size:14];
CGRect ansRect = [dict[@"answer"] boundingRectWithSize:boundingSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes: @{NSFontAttributeName:font} context:nil];
return (qstnRect.size.height+ansRect.size.height+30); // 30 is the height of another label whose height is fixed to 30pts.
}
Upvotes: 0
Reputation: 978
If you have added UILabel inside Storyboard to UITableviewCell, make sure you have did all changes as per below instractions:
In Storyboard do this changes: Make sure you have given top, bottom, leading and trailing constrains to the UILabel to UITableViewCell contentView.
In Controller for UITableViewDelegate heightForRow method:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
tableView.estimatedRowHeight = 50;
return UITableViewAutomaticDimension
}
This will help to solve above requirement.
Upvotes: 0
Reputation: 4365
Your problem is your cell height and tableview row height are different you can solve this in 2 ways
Implementing AutoLayout to your custom cell, by this way your cell will grow/shrink according to your row height.
The second method is you can make your row height as same as cell height
by using -(CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
delegate method
Just return your cell height in the above delegate method it will solve your problem.
Upvotes: 0
Reputation: 9503
Follow the below steps :
numberOfLines
property to zeroImplement these two tableView delegates methods
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat{
return 55
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return UITableViewAutomaticDimension
}
Now run your project and check.
Expected output:
Upvotes: 4
Reputation: 8371
There are many ways to design Layout for Cell in tableview with dynamic height
here I am sharing some links related to dynamic height and cell autolayout will help to get out form your issue.
Raywenderlich : https://www.raywenderlich.com/129059/self-sizing-table-view-cells
AppCoda : http://www.appcoda.com/self-sizing-cells/
Some answer form SO :
Hope this will help you to solve your issue.
Upvotes: -1