I have taken UITableView
into UIScrollView
now I want to set static height to tabelview
and set contentSize
to ScrollView
because UITableView
calling cellForRowAtIndexPath
every time when I scroll tableview
.
I am setting tableviewcell
size dynamically using below code.
Now question is: How and where i can set total height to tableview
and content size to my scrollview
?
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *itemDataDic = [resultArray objectAtIndex:indexPath.row];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:15.0];
CGSize constraintSize = CGSizeMake(275.0f, MAXFLOAT);
CGSize labelSize = [[itemDataDic objectForKey:@"offer_title"] sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
if (labelSize.height > 30.00f)
{
totalHeight = totalHeight + 325;
return 325;
}
else
{
totalHeight = totalHeight + 306;
return 306;
}
}
'heightForRowAtIndexPath' is called many times for a cell, therefore, totalHeight is calculated wrong in this way.
You need to calculate the height of your table on init and when the data has been changed.
-(void) reloadAndResizeTable
{
CGFloat totalHeight = .0f;
for (NSMutableDictionary* itemDataDic in resultArray) {
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:15.0];
CGSize constraintSize = CGSizeMake(275.0f, MAXFLOAT);
CGSize labelSize = [[itemDataDic objectForKey:@"offer_title"] sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
if (labelSize.height > 30.00f)
{
totalHeight = totalHeight + 325.0f;
return 325;
}
else
{
totalHeight = totalHeight + 306.0f;
return 306;
}
}
CGRect frame = [yourTableView frame];
[yourTableView setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, totalHeight)];
[yourTableView reloadData];
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments