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.
Related
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.
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;
}
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;
}
I want to adjust the row height of a UITableView according to the cell in that row.
Initially, I tried to use
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
However, the problem is, when the table is loading, the calling flow is seemed to be:
First call:
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Then call:
(UIViewTableCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
which means, before my cell is generated, I have to tell the table the height of its row.
However, what I want is exactly the opposite, i.e., after my cell is generated, I tell the table the height of this rows.
Is there any method to achieve this?
Make an array of heights for every row based on the data from tableView: cellForRowAtIndexPath: and in the tableView heightForRowAtIndexPath: just take the height value from that array.
Declare in your implementation file:
NSMutableArray *heights;
In viewDidLoad: initialise it:
heights = [NSMutableArray array];
In tableView: cellForRowAtIndexPath: set the height for each row:
[heights addObject:[NSNumber numberWithFloat:HEIGHT]];
And in the tableView heightForRowAtIndexPath: return required height:
return [heights objectAtIndex:indexPath.row];
This should work because tableView: cellForRowAtIndexPath: is called for every cell and then for every cell tableView heightForRowAtIndexPath: is called.
may be the only solution is make a variable into the .h file like
#property (nonatomic) int height
initialise it into the viewDidLoad like
self.height = 40;
and then return this variable into the
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return self.height;
}
then in your tableView: cellForRowAtIndexPath: update this variable like your new height and then reload the [self.yourTable reloadData] in your viewDidAppear