I want to change the height of my tableview from another viewcontroller based on the sum of its cells' heights, as they are dynamic. Is it at all possible? Thanks
Add-on:
What I basically have is a UserProfileViewController that has a containerview on half of the screen. There I add different other viewcontrollers:
In the case of the wall button this is how I add the viewcontroller and it's subsequent tableview:
- (IBAction)wallButtonPressed:(id)sender
{
//Check if there is an instance of the viewcontroller we want to display. If not make one and set it's tableview frame to the container's view bounds
if(!_userWallViewController) {
self.userWallViewController = [[WallViewController alloc] init];
// self.userWallViewController.activityFeedTableView.frame = self.containerView.bounds;
}
[self.userWallViewController.containerView addSubview:self.userWallViewController.activityFeedTableView];
//If the currentviewcontroller adn it's view are already added to the hierarchy remove them
[self.currentViewController.view removeFromSuperview];
[self.currentViewController removeFromParentViewController];
//Add the desired viewcontroller to the currentviewcontroller
self.currentViewController = self.userWallViewController;
//Pass the data needed for the desired viewcontroller to it's instances
self.userWallViewController.searchURLString = [NSString stringWithFormat:@"event/user/%@/", self.userID];
self.userWallViewController.sendCommentURLString = [NSString stringWithFormat:@"event/message/%@", self.userID];
[self.userWallViewController.activityFeedTableView reloadData];
self.userWallViewController.totalCellHeight = ^(float totalCellHeight){
self.scrollView.contentSize = CGSizeMake(320.0, totalCellHeight);
CGRect newFrame = self.userWallViewController.containerView.frame;
newFrame.size.height = totalCellHeight + 33.0;
self.userWallViewController.containerView.frame = newFrame;
self.userWallViewController.activityFeedTableView.frame = self.containerView.bounds;
};
//Add this containerview to the desired viewcontroller's containerView
self.userWallViewController.containerView = self.containerView;
//Add the needed viewcontroller and view to the parent viewcontroller and the containerview
[self addChildViewController:self.userWallViewController];
[self.containerView addSubview:self.userWallViewController.view];
//CLEAN UP THE CONTAINER VIEW BY REMOVING THE PREVIOUS ADDED TABLE VIEWS
[self.userFansViewController.userSimpleTableView removeFromSuperview];
[self.fanOfViewController.userSimpleTableView removeFromSuperview];
[self.userPublishedMovellaListViewController.gridView removeFromSuperview];
[self.userPublishedMovellaListViewController removeFromParentViewController];
self.userPublishedMovellaListViewController = nil;
}
and in that viewcontroller this is where I initialize my tableview:
-(UITableView *)activityFeedTableView
{
if (!_activityFeedTableView) {
_activityFeedTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 850.0) style:UITableViewStylePlain];
}
return _activityFeedTableView;
}
I am calculating he total sum of the cell's height, the problem is that the cell's height method is called way after the getter of te tableview is called. So I would need some sort of way to know when the cells' height method is done for all cells and then I can resize my tableview. Thanks
Rob's solution is very nice, only thing that in his
-(void)adjustHeightOfTableview
method the calling ofdoes nothing, it just returns a flag, instead calling
will make the desired effect.
for resizing my table I went with this solution in my tableview controller witch is perfectly fine:
The resizing part is in a method but here is just so you can see it. Now the only problem I haveis resizing the scroll view in the other view controller as I have no idea when the tableview has finished resizing. At the moment I'm doing it with performSelector: afterDelay: but this is really not a good way to do it. Any ideas?
I found adding constraint programmatically much easier than in storyboard.
create your cell by xib or storyboard. give it's outlet's contents. now call it in CellForRowAtIndexPath. eg. if you want to set cell height according to Comment's label text.
so set you commentsLbl.numberOfLine=0;
so set you commentsLbl.numberOfLine=0;
then in ViewDidLoad
and now
Use simple and easy code
This can be massively simplified with just 1 line of code in viewDidAppear: