table cell height issue iOS? - ios

I have custom table view.I have added 4 views in that
1.ImageView
2.Three labels
Now I want that image should increase in size for multiple devices.For that i have given it below constraints.Proptional height is 176:339
Now in order to increase the height of the image i have increase height of table i used below code.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(!self.customCell)
{
self.customCell = [self.tableview dequeueReusableCellWithIdentifier:#"tableCell"];
}
CGFloat height;
height=self.customCell.img_main.frame.size.height;
height=height+self.customCell.label_one.frame.size.height;
height=height+self.customCell.label_two.frame.size.height;
height=height+self.customCell.label_three.frame.size.height;
height=height+self.customCell.dev_name.frame.size.height;
NSLog(#"height is %f",self.customCell.img_main.frame.size.height);
return height;
}
The heigh of image is always 176 which i set in interface builder for 4 inch screen.
Why the height of both images & table view cell is not increasing ?

This is for your second question, if you want to change the image view's height base on the image it display, i suggest to use Height Constraint instead of Proportional Height Constraint, the calculation will be easier. When you get the image, and know the image size, so you calculate and get the size of the image view, and then update the Height Constrains. Done.
drag the constraint to your cell
update the constraint when image setted
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellId" forIndexPath:indexPath];
cell.image = image;
cell.imageViewHeightConstraint.constant = [self calcuteSizeWithImage:image]; //you need to do the calculation yourself
return cell;
}

Related

What is the best way to conditionally display an image created via storyboard?

I'm creating a detail view screen in an app. The content that is being shown in detail may, or may not have an image. I am using estimated row height when displaying my cells.
The xib has the following constraints:
Trailing Space to superview
Leading Space to superview
Bottom Space to superview
Top Space to superview
Height <= 200
This works great when I've got an image displayed, however if there is no image the cell is rendering with a height of about 40 and i'm getting a warning.
Warning once only: Detected a case where constraints ambiguously
suggest a height of zero for a tableview cell's content view. We're
considering the collapse unintentional and using standard height
instead.
What is the best way to resolve this? I really don't want to try to conditionally change the number of rows the table has based on an image attribute.
Ideally I'd like constraints that are such that use the above constraints, image displayed edge to edge, clipping is fine, with a max height of 200. Or if there is no image the height is 0.
Code in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
EntityContentObject *object = (EntityContentObject *)self.resultsArray[0];
self.imageURLString = object.entityPicture;
NSLog(#"IMAGE URL STRING: %#", self.imageURLString);
ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ideaImgCell"];
if (!cell)
{
[tableView registerNib:[UINib nibWithNibName:#"ImageTableViewCell" bundle:nil] forCellReuseIdentifier:#"ideaImgCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"ideaImgCell"];
}
if([self.imageURLString isEqualToString:#""]){
// no image to display;
}else{
[cell.ideaImage setImageWithURL:[NSURL URLWithString:self.imageURLString] placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
}
NSLog(#"CELL IDEA IMAGE HEIGHT: %#", NSStringFromCGRect(cell.ideaImage.frame));
return cell;
I think you must have to see this https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html
write these lines in your project
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(no image){ // put condition
return 0;
}
else{
return 200;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
if(no image){
return 0;
}
else{
return UITableViewAutomaticDimension;
}
}

Dynamic UIlabel inside UITableViewCell

The user can make a comment which can be can be any size or height. The comment label's dynamic content should fit into the cell.
I have allocated my label inside UITableViewCell with a key value.
cell.CommentLabel.text=[ResDiction objectForKey:#"Comment"];
cell.TimeLabel.text=[ResDiction objectForKey:#"CreateDate"];
cell.UserName.text=[ResDiction objectForKey:#"CreateUserName"];
cell.UserName.adjustsFontSizeToFitWidth=YES;
cell.CommentLabel.adjustsFontSizeToFitWidth=YES;
cell.CommentLabel.numberOfLines = 8;
cell.TimeLabel.adjustsFontSizeToFitWidth=YES;
How do I let the label's content determine the cell height?
You have to use
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableviwCell* cell=[tableview cellForRowAtIndexPath:indexPath];
cell.CommentLabel.text=[ResDiction objectForKey:#"Comment"];
cell.TimeLabel.text=[ResDiction objectForKey:#"CreateDate"];
cell.UserName.text=[ResDiction objectForKey:#"CreateUserName"];
cell.UserName.adjustsFontSizeToFitWidth=YES;
cell.CommentLabel.adjustsFontSizeToFitWidth=YES;
cell.CommentLabel.numberOfLines = 8;
cell.TimeLabel.adjustsFontSizeToFitWidth=YES;
float height= cell.cell.CommentLabel.frame.size.height+TimeLabel.frame.size.height+(all your UIFields you have used)
return height;
}
This method is will return the height for the cell while it is being created.
The code inside will do calculate height of each uiField and combine them, this will work if you are arranging like stacks in UITableview cell, or you have to modify the code inside the function as per you arrangement.

custom cell height from UIImageView size?

I have a UIImageView inside a custom cell. If there is no image to display in cell then cell height should automatically decrease.(ImageView Height should be zero in that case). How to specify constraint for this in xib ?
Your UITableView data source should represent the existence of this image.
You should use the following delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.dataSource[indexPath.rox].imageExists) {
return 50.0; // Change to your value with image
}
return 10.0; // Change to your value without image
}
Your UITableViewDelegate should implement tableView:heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// add your condition here if image exit or not and set cell size dynamically
return [indexPath row] * 20;
}
You will probably want to use NSString's sizeWithFont:constrainedToSize:lineBreakMode: method to calculate your row height rather than just performing some silly math on the indexPath.
Here is good sample code for dynamic size cell.

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");

How to set UITableViewCell height depending on screen size

I download data from the web, it consists of text and image. I'm putting this data into UITableView, and I want image to be a background of UITableViewCell.
All the images has the same resolution 620x350.
All the iPhones have different screen sizes so I need to scale image's width and height depending on screen size,and also do this to UITableViewCell.
What are the best solutions?
Thank you.
The BEST and clean solution, it is just playing with the height of the cell, letting the image be resized automatically by Autolayout.
It is easy:
In your cell, add an UIImageView and set the size you want to have when the cell has the actual height;
Set as constraints of this UIImageView, at least top, bottom and one of the lateral border (right or left); set also the constraints to keep the proportion width/height;
At this point, when the cell grow, the image grow in height, and to keep the proportion (thanks constraint) it is resized the width as well, having ALWAYS the correct and proportioned size.
Now therefore, you have just to write the code for the cell's height:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
static CGFloat cellDefaultHeight = /*the default height of the cell*/;
static CGFloat screenDefaultHeight = /*the default height of the screen i.e. 480 in iPhone 4*/;
CGFloat factor = cellDefaultHeight/screenDefaultHeight
return factor * [UIScreen mainScreen].bounds.size.height;
}
Obviously, set the contentMode of the UIImageView to AspectFill.
Simply use - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat aspectRatio = 350.0f/620.0f;
return aspectRatio * [UIScreen mainScreen].bounds.size.width;// assuming that the image will stretch across the width of the screen
}
You can use the -tableView:heightForRowAtIndexPath: method in HTTableViewDelegate to return the height for each cell. In your case, you will want something like this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [UIScreen mainScreen].bounds.size.height;
}

Resources