I have UITableViewCell its height is set to 60. UITableViewCell content view height is set to 59.5 automatically and it is not possible to edit it in the size inspector. This is the reason all the text in those cells becomes blurry.
How can I fix this issue?
use this method it will help you
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return height you want for cell ;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Use this method
Return Height from heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return x; //CGFloat
}
May be blur you seeing is because of Scale of simulator(if you are using simulator).
So, for simulator check in Window --> Scale, is it 100% or not.
Related
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.
When I set tableView.rowHeight to 57, one of the cells always have the wrong row height. What could be the reason? Also I've tried implementing the delegate method as well with no luck.
In your view controller just implement the following delegate method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 57f;
}
your custom cell height and heightForRowAtIndexPath returned height should be same.
suppose custom cell height is 65.and
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 65f;
}
What is the current best practice to have dynamic items in iOS.
As you can see the headline can change in length and height dependant on the string length. I want to shift everything down according to this. It's kind of like Facebook Newsfeed.
What is the best practice? I would prefer using constraints to achieve this.
If you want to add the dynamic content in UITableViewCell, you have to do following things,
1.You should calculate the dynamic Content Size and set UITableViewCell hight
Example:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
float height = [#"" sizeWithFont:[UIFont systemFontOfSize:10.0] constrainedToSize:CGSizeMake(320.0, MAXFLOAT)].height;
return height;
}
2.You should adjust your UI elements frame in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
Im trying to make some of my cells bigger in height. i tried using
CGRect rect = cell.frame;
NSLog(#"before height: %f",rect.size.height);
rect.size.height +=20;
cell.frame = rect;
NSLog(#"AFTER height: %f",cell.frame.size.height);
in
cellForRowAtIndexPath
and
willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath
*)indexPath
The log shows that the values have changed but it doesnt show any change in the simulator.
Thanks for the help
use tableView:heightForRowAtIndexPath method.
Apple docs clearly explains what to do. UITableView class reference
Every tableView has a delegate property.Set it to your viewController and implement above method. Its signature is
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
So, based on indexPath, return whatever the height you desire.
If you want constant height for all rows, you can use rowHeight property of UITableView.
Use heightForRowAtIndexPath of UITableViewDelegate. Example:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row == _pages.count - 1 ? 408 : 450;
}
To make some cells bigger, you should implement the method tableView:heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (<SOMETHING>) {
return height1;
} else {
return height2;
}
}
tableView:cellForRowAtIndexPath: is for configuring the content of the cell that the tableView needs to display.
Note that tableView:heightForRowAtIndexPath: has performance implications if you have a really large table (1000+ entries), this method is called on every row when the table view displays.
I've designed a custom table cell as follows:
However, it is rendering as follows:
as you see, the second label is going in the next cell. Any idea how can I solve this issue and make the cell appear as I designed it?
You need to implement UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
This method:
Asks the delegate for the height to use for a row in a specified
location.
Give a minimum height for each row here. Also if different row have different height according to the content, you have to calculate the height according to the content in that case. Something like this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *stringText=[array objectAtIndex: indexPath.row];
CGSize size = [stringText sizeWithFont:[UIFont fontWithName: "yourFont" size: fontSize] constrainedToSize:maxCGSize];
return labelSize.height + padding;
}