dynamic uitableviewcell height according to custom cell height - iphone

I am wondering, is there a way to read the height of the custom tableview cell you are going to use in your uitableviewcell which you can then say to your uitableviewcell be this ---> height?

I am not fully understand your question but do you want to set the height for each custom cell ? If yes then you can do it by :
Let say I have custom cell at index 0 rest are my default cells or can be custom cell as well .
- (CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0 ) {
return 105.0;
}else {
return 80.0;
}
}

No, the height that you set in the xib/storyboard is for display purposes only and doesn't transfer over to code.
The only way to change the height is from your custom subclass, by overriding tableView:heightForRowAtIndexPath:.
If you have a lot of different heights, you could always subclass UITableViewCell and add a height property which tableView:heightForRowAtIndexPath: uses when it sees your subclass. Then, in your storyboard, select your cell, go to the identity inspector, and add a User Defined Runtime Attribute for height with the value that you need.

Related

Within cellForRowAtIndexPath getting height of cell when cells are variable height

I create custom cells within my tableview some have images and are tall some are just text. The height of the cells are calculated in heightForRowAtIndexPath, which I beleive is done before cellForRowAtIndexPath is called. I want to place an imageview at the bottom of the cell regardless of heigh, but I am not sure how to get the calculated height from within cellForRowAtIndexPath?
Too late for an answer..
But, like #user216661 pointed out, the problem with taking the height of the Cell or the ContentView is that it returns the cells original height. Incase of rows with Variable height, this is an issue.
A better solution is to get the Rect of the Cell (rectForRowAtIndexPath) and then get the Height from it.
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
UITableViewCell *aCell = (UITableViewCell *)[iTableView dequeueReusableCellWithIdentifier:aCellIdentifier];
if (aCell == nil) {
CGFloat aHeight = [iTableView rectForRowAtIndexPath:iIndexPath].size.height;
// Use Height as per your requirement.
}
return aCell;
}
You can ask the delegate, but you'll be asking twice since the tableView already asks and sizes the cell accordingly. It's better to find out from the cell itself...
// in cellForRowAtIndexPath:, deque or create UITableViewCell *cell
// this makes the call to heightForRow... and sizes the cell
CGFloat cellHeight = cell.contentView.bounds.size.height;
// alter the imageView y position (assuming the rest of the frame is correct)
CGRect imageFrame = myImageView.frame;
imageFrame.y = cellHeight - imageFrame.size.height; // place the bottom edge against the cell bottom
myImageView.frame = imageFrame;
You are allowed to call heightForRowAtIndexPath yourself! Just pass the indexPath from cellForRowAtIndexPath as an argument and you can know the height of the cell you are setting up.
Assuming you are using a UITableViewController, just use this inside cellForRowAtIndexPath...
float height = [self heightForRowAtIndexPath:indexPath]

how to obtain the UITableViewCell within heightForRowAtIndexPath?

How does one obtain the UITableViewCell when within the heightForRowAtIndexPath method, i.e. given the indexPath?
(then I could access the content views I have created to add their heights up)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// How to get the UITableViewCell associated with this indexPath?
}
thanks
EDIT: In fact is there really a valid way to do this? When I put some NSLog statements it seems that heightForRowAtIndexPath it called several times before the calls to cellForRowAtIndexPath (which is where I set up the UILabels in the cell)? This kind implies that I may be tried to use a technique that will not work, i.e. I was hoping in heightForRowAtIndexPath to access the already created labels in each cell to get their heights and add them together for the overall cell row height, HOWEVER if they haven't been set up yet (within cellForRowAtIndexPath) then I guess my approach can't really work?
You can use the tableview's delegate instead of the tableView itself.
id cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
Check the answer here
UPDATED
In new iOS versions and using correct auto constraints you don't need to reference the cell anymore to calculate the cell's height.
Check here
https://www.raywenderlich.com/129059/self-sizing-table-view-cells
This question has no sense because in
heightForRowAtIndexPath
no cells are created yet. Here's how tableView works:
TableView asks it's datasource how many sections it will have. -numberOfSectionsInTableView
Asks it's datasource how many rows each section will have (to know how big scroller should be etc.) -numberOfRowsInSection
Asks it's delegate height of each visible row. To know where cells will be located. - heightForRowAtIndexPath
Lastly it asks datasource to give it a cell to display at given index path -cellForRowAtIndexPath
So you can't access to cells from heightForRowAtIndexPath because with a most likely cell is not created yet.
However in case I misunderstood your question go try:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
The obvious answer is to call cellForRowAtIndexPath, but you may have already discovered there are some issues with that. See this question if you haven't already: UITableView flexible/dynamic heightForRowAtIndexPath
For my last project I used a custom subclass of UICell and implemented a method like this. I then called this from table:heightForRowAtIndexPath: (after looking up the content for that row).
+ (CGFloat) heightOfContent: (NSString *)content
{
CGFloat contentHeight =
[content sizeWithFont: DefaultContentLabelFont
constrainedToSize: CGSizeMake( DefaultCellSize.width, DefaultContentLabelHeight * DefaultContentLabelNumberOfLines )
lineBreakMode: UILineBreakModeWordWrap].height;
return contentHeight + DefaultCellPaddingHeight;
}
i would suggest you to calculate the correct height of a row in table:heightForRowAtIndexPath: by using your data structure (Height of text,images,mutliline text etc..).
And change the component height in their -(void) layoutSubviews method.
If you want to access the cells use tags. But as #Jhaliya has said it is better to calculate the height based on the content of the cell. If your regard is with the position of the cell then I would suggest you to use scroll view and implement the cells yourself wherever you want.
For iOS8:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
}
OR
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
But for iOS7, the key is calculate the height after autolayout,
func calculateHeightForConfiguredSizingCell(cell: GSTableViewCell) -> CGFloat {
cell.setNeedsLayout()
cell.layoutIfNeeded()
let height = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize).height + 1.0
return height
}
Note:
1) If multiple lines labels, don't forget set the numberOfLines to 0.
2) Don't forget label.preferredMaxLayoutWidth = CGRectGetWidth(tableView.bounds)

How to set the Cell height as dynamically in iPhone?

In my apps, i have created table view and displayed the images and the labels in the table view. I have downloaded the image and set the image height into the another class and i have passed the image height value using delegates. In my problem is, heightForRowAtIndexPath method is call very earlier and it never calls again. So how i set the cell height depends on the image height?
//appDelegate.imageHeight - Image height delegate value.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(the height of the image is --------------------------%f, appDelegate.imageHeight);
In Console:
the height of the image is --------------------------0.000000
the height of the image is --------------------------0.000000
the height of the image is --------------------------0.000000
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(the height of the image is --------------------------%f, appDelegate.imageHeight);
In Console:
the height of the image is --------------------------98.567039
the height of the image is --------------------------300.000000
the height of the image is --------------------------232.288406
}
Is any possible to call the heightforRowAtIndexPath method inside the cellForRowAt method. Because i have set the cell frame size and height depends on the image height. So how can i set the cell height depends on the image height?, please guide me.
Thanks!
heightForRowAtIndexPath method is called before cellForRowAtIndexPath, so you need to know height of images you are displaying in your cells. Maintain the dictionary or array that contains height of images which get passed to this class and use it to set the height of cell accordingly
here are some suggestions UITableView flexible/dynamic heightForRowAtIndexPath

Custom width for cell in UITableViewStyleGrouped tableviews

In the iPhone SDK, is there a way to customize the width of the cells in a tableview that is having the style of UITableViewStyleGrouped?
The problem is that I am using an custom background image (width of 290px) for the cell.backgroundView but apparently that gets stretched. I want to be able to set the custom width of this cell so the background image doesn't stretch.
Just resize the tableView as per your requirement ..........either from Nib or programmatically.....
size of cell depends on the width of table.
because there is not such any method in IPhoneSDK
-(CGFloat)tableView:(UITableView *)tableView widthForRowAtIndexPath:(NSIndexPath *)indexPath
{
// width
return 300;
}
Can you not call the delegate method
-(CGFloat)tableView:(UITableView *)tableView widthForRowAtIndexPath:(NSIndexPath *)indexPath
{
// width
return 300;
}

How to shrink the spacing between Cell Header and Cell Text in UITableView

Is it possible to shrink the spacing between a cell header and cell text? Looking at the example below I'd like the header to be closer to the top of the cell that contains the other text.
Sorry, I don't have high enough reputation to embed images.
http://www.freeimagehosting.net/uploads/6afdd58c2f.jpg
Just implement tableView:heightForHeaderInSection: in your controller and return your desired height.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 50.0f;
}
I assume that you are using a standard cell style and textLabel. You could change the frame.origin of the label, or create a custom cell from a nib with the exact layout you prefer.
You need to use method heightForHeaderInSection for defining space between header & cell text.
You can also change it depending on different sections for eg. at some sections you may need to show more distance & under some, you don't want to show gap.
For such case you can use CGFLOAT_MIN which is 0.000001f.
Giving you an example, how you can use different section with different header heights
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0 || section == 2)
{
return 55.0;
}
else
{
return CGFLOAT_MIN;
}
}
Hope it will help you.