Cell height equal to image width - ios

I've created cell with 4 different imageViews where i've created dynamic constraints, so they will change the width and height equal to the device size, but since cell is not dynamic then it won't change the height of the imageView. Therefore I want to set the cell Height equal to the image Width, so the images always will have the same width and height.
In heightForRowAtIndexPath, I've tried to return following, but this is giving me an error: (lldb)
let cell = tableView.dequeueReusableCellWithIdentifier("ImageViewCell", forIndexPath: indexPath) as ImageViewCell
return cell.image1.frame.width
How can I do this?

you cant call cell object within heightForRowAtIndexPath because it creates infinite loop as cellForRowAtIndexPath calls heightForRowAtIndexPath. secondly, adjust width using constraints
as function name clearly indicates that it is for height.
If you are sure about the height of the control, then use this
return 90.0 //its optional if height is fixed
else
add constraints for top, bottom,left ,right, width and height constraint, and use >= condition for height constraint.

I think you need to use to Tableview's Delegate to set its cell height. maybe you can try somethin like that:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.row) {
case 0:
return image.frame.size.width;
break;
case 1:
return image.frame.size.width;
break;
default:
return 44;
break;
}
}
I think you need to get the size of UIimage from your datasource. not from your cell.

Related

Self-Sizing TableView Cells based on two subview's height

TableView Description
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 60.0
Cell Description
I have a cell with 2 Labels(Title, Description) inside a ContainerView and 1 ImageView as below. Cell height will vary based on Description Label’s content.
Contraints of all views
There are two cases that I should handle
ContainerView.height greater than (ImageView.height + ImageView.Top + ImageView.Bottom). Here cell's height will be based on ContainerView.height
ContainerView height less than (ImageView.height + ImageView.Top + ImageView.Bottom). Here I expect Cell should consider (ImageView.height + ImageView.Top + ImageView.Bottom) as the height and make ContainerView vertically centre to Cell.
Expected Result in both the cases
Problem
If I set constraints for 1st case then 2nd case is not working and vice versa (I’m aware that by removing ContrainerView.Top, Bottom and making it Vertically Centre to SuperView case 2 result can be achieved)
Is there a way to achieve expected result in both the cases by using same set of IB constraints and UITableViewAutomaticDimension?
Give fixed height and width to the image view . Otherwise let the tableViewCell know the top and bottom of the imageview. So that it can calculate the correct cell height
First make sure that you are using self-sizing cells:
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html
Make Top and Bottom constraints for both image view and the container view to the edges of the cell and make them >=.
Alternatively, you could try Horizontal Stack View and make rigid (highest priorities) constraints to each edge of the cell.
Use these delegates,
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100; // height of default cell
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
Edit
Try this one.
Your view hierarchy should be like this
ImageView Constraints
Add a view and put both labels into it.
Label Container contraints
Label Title constraint
Label Description constraint
Edit Output

How to create dynamic tableview cell with dynamic tableview height in iOS

I want to increase tableview cell and tableview height based on content.
Suppose tableview contain 2 record and his 1st cell height is 100 and 2nd cell height is 25 then tableview height should be 100+25=125.
How to achieve this functionality?
Thanks in advance.
You can definitely do that,
First make sure your constraint of cells subView must set to top to bottom in order to calculate the height required for the cell.
Make sure your delegated are set as below
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
Set height constraint of your tableView and make outlet of that constraint.
Add below method to your class where you want to resize your tableView dynamically.
- (void)adjustHeightOfTableview
{
CGFloat height = self.tableView.contentSize.height;
//CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y;
/*
Here you have to take care of two things, if there is only tableView on the screen then you have to see is your tableView going below screen using maxHeight and your screen height,
Or you can add your tableView inside scrollView so that your tableView can increase its height as much it requires based on the number of cell (with different height based on content) it has to display.
*/
// now set the height constraint accordingly
self.constraintHeightTableView.constant = height;
//If you want to increase tableView height with animation you can do that as below.
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
}
Call this method when you are ready with the dataSource for the table, and call the method as
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
//In my case i had to call this method after some delay, because (i think) it will allow tableView to reload completely and then calculate the height required for itself. (This might be a workaround, but it worked for me)
[self performSelector:#selector(adjustHeightOfTableview) withObject:nil afterDelay:0.3];
});
If you are running iOS 8+,
You can use:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80 // your desired or expected height
properties.
for this to take effect you should not have any height set in heightForRowAtIndexpath
You should set the cell constraints i.e., constraints for the elements present inside cell, so the set constraints are enough for the tableviewcell to calculate it's height in run time
Solution in swift 3
Step 1.
Set the top, bottom, leading and trailing constraints (do not make it constant height, but do set a constant height constraint as of now).
Now we gonna change this height dynamically based on the number of cells and its height.
Step 2.
Drag an outlet of that height constraint to the class, and copy this function anywhere in the class.
func adjustHeightOfTableview() {
let height: CGFloat = tableview.contentSize.height
self.outLetOfTheHeightConstraint.constant = height
}
Step 3.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
DispatchQueue.main.async {
self.tableview.reloadData()
self.perform(#selector(self.adjustHeightOfTableview))
}
}
self.tableView.frame = CGRectMake(self.tableView.origin.x, self.tableView.origin.y, self.tableView.frame.size.width, 125);

UITablViewCell dealing with changing the size of the cell

Iam creating a custom UITableViewCell programatically , I need the height of the cell (which I changed) , at my custom cell I get the cell's height by self.contentView.bounds.height , which gives me the original height (not the changed one ) , how can I get the changed height ???
You MUST calculate cell height in
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Otherwise tableView uses Row Height from Interface Builder Size inspector for tableView.
You can use this
CGRect frame = [tableView rectForRowAtIndexPath:indexPath];
//To get the height
NSLog(#"row height: %#", frame.size.height");

Custom UITableViewCell height based on descendant view (xCode 6, iOS8)

I have a custom view inside a prototype cell, with a label and a UIImageView that can both be variable heights. What is the best approach to get the table cells to adjust to the view's height? It seems I have to set a height to the table cell and anything else I try won't reset the size.
i've tried the concept below with no luck.
tableView.estimatedRowHeight = 260.0
tableView.rowHeight = UITableViewAutomaticDimension
As far as I know, the height for UITableViewCell should be fixed. You could have more than 1 type of UITableViewCell inside the same UITableView. For example, the section 0 has a unique type of UITableViewCell, the UITableViewCell for the other sections have another type. Then inside the TableView Delegate method heightForRowAtIndexPath, you may set the height for the different types of UITableViewCell.
You could do something like this:-
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section==0)
return 40;
else
return 60;
}

heightForRowatIndexPath does not set cell height

I am trying to create a grouped table view with 3 groups, and each group has cells that are a different height. I've set up 3 prototype cells in my Storyboard with the appropriate heights, and have set up my UITableViewController to return the appropriate heights for each row. However for some reason all of my cells are only 1 pixel tall when they appear on screen.
I've stepped through the code and the correct height and cell is returned for each row. I've also verified that the frame of each cell is correct before it is returned. However calling rectForRowAtIndexPath reveals that the rect for each row is only 1 pixel tall.
This problem goes away entirely when using a constant cell height. In that situation the cells display perfectly, some of them are just too short/tall since they should be variable height.
Any ideas what is going on here? I'd like to undertand how the rect for each row is determined when using heightForRowAtIndexPath.
Code for heightForRowAtIndexPath:
- (NSInteger)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case 0:
return 80.0;
break;
case 1:
return 260.0;
break;
case 2:
return 110.0;
break;
case 3:
return 114.0;
break;
default:
break;
}
return 0;
}
You have declared tableView:heightForRowAtIndexPath: to return NSInteger.
That should be CGFloat.
simply to increase the height for row .simply write the method for height for row at indexpath an simply written
if (indexpath.section== 0)
return 50;

Resources