UITablViewCell dealing with changing the size of the cell - ios

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

Related

table cell height issue 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;
}

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 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;
}

Increasing Tablview Cell Height and Print the data at specific row

I have one UItableview and I have one customCell for printing the data in the UITable.
Now, I want to put more data in specific cell. When I select on cell, it should increase the height of cell and put that data. How is it possible.?
In DidSelect make the height of particular selected cell to your required cell.
And reload the table.
This is helpful : How to programmatically increase UITableView cell's height in iPhone?
Call reload table data by changing the parameters of row and to find the height of a cell after adding data use:
CGSize size=[youstring sizeWithFont:yourlabel.font constrainedToSize:labelfontsize lineBreakMode:lineBreakmodestyle];
And get height from size.height and return the height in heightForRowAtIndexPath method while reloading tableview.
what you should do is First Calculate the Size of your content for which you have to increase the cell height
//Suppose here is your string which you want to show
-(float) calculateHeight:(NSString *)dataStr
{
CGSize size = [dataStr sizeWithFont:[UIFont fontWithName:#"Helvetica" size:17] constrainedToSize:CGSizeMake(280, 999) lineBreakMode:UILineBreakModeWordWrap];
NSLog(#"%f",size.height);
return size.height + 10;
}
This Above function will return the height according to your Text Size.
Now just have to call this function on the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
delegete and reload the tableview and give the size you calculated with the above function at heightForRowAtIndexPath delegete.
Try this sample if I've understood what you want: https://github.com/Dmitriy837/changeTableRowHeighWhenTaped
I suggest you subclass UITableView and support additional property with cell heights in it and change the cell's height when it's taped.

Setting UITextView height based on contentSize inside a UITableViewCell only works after the initial table load

I have a UITextView inside a UITableViewCell. I am attempting to set the height of the UITextView it based on the content size. Inside my cellForRowAtIndexPath method I have:
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
....
cell.text.text = textString; // cell.text is the UITextView
CGRect frame = cell.text.frame;
frame.size.height = cell.text.contentSize.height;
cell.text.frame = frame;
return cell;
There are other objects on the custom cell, but nothing has constraints. The interesting thing is that when the table view is first loaded, the UITextView does not resize. However, when another cell is loaded by scrolling down or back up, the UITextView resizes to the correct height based on the above code. Why isn't the initial table loading correctly.
The UITextView has a default width and height size. Let me know if I can add more details. I am not implementing heightForRowAtIndexPath, but the testing I am doing has not exceeded the default cell heights so this should not matter anyways.
Did you log the value of cell.text.contentSize.height ?
At that moment you don't get the correct value, I think you should set the height on the following UITableView callback:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
Try to add [cell.text layoutIfNeeded] before calling cell.text.contentSize
The contentSize will be unknown by the UITextField until it has been drawn. Calling layoutIfNeeded should force the computation of layout size.
If finaly made it work with this, try it
cell.text.attributedText = [[NSAttributedString alloc]initWithString:textString];
float textViewWidth = cell.text.frame.size.width;
[cell layoutIfNeeded];
cell.text.contentSize = [cell.text sizeThatFits:CGSizeMake(textViewWidth, FLT_MAX)];
[cell.text sizeToFit];

Resources