UITableView cells doesn't fit UITableView height - objective-c

I download Json data and then I populate my custom UITableView, the problem is that even if I calculate the height of the tableview based on the amount of elements I download the cells doesn't fit the whole height of the tableview, So my final situation is a tableview with n elements that has height of n*100 but the cells just fits a part of it.
This is the code I used to calculate the height:
//callback that contains the items I download
-(void)productsDownloaded:(NSArray *)items
{
_productsDetails = items;
CGFloat height = 100;
height *= items.count;
CGRect tableFrame = _customTableView.frame;
tableFrame.size.height = height;
_customTableView.frame = tableFrame;
[_customTableView reloadData];
}
I also tried:
_customTableView.alwaysBounceVertical = NO;
_customTableView.scrollEnabled = NO;
But with no results.. How can I tell the cells to fit the height of the cell?

To achieve a scrollable table view, you have to use the tableView:heightForRowAtIndexPath: delegate method to specify the size (height) or your cell, not just change the frame of your table view.
Example:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//TODO: Calculate cell height
return 60.0f;
}
To have all the cells visible on one screen, the total height of the table view cells has to be equal to the height of the table view, thus the height of one table view cell should be:
CGFloat cellHeight = CGRectGetHeight(tableView.frame) / numberOfCells;
However, I really don't understand why you would want to do this. The whole reason of a table view is to have a fixed height for cells and scroll (Like the Settings app does it).

Related

collectionview inside tableview on expand collapse with dynamic cell height. objective c

i need when i expand-collpase tableview indide tableviewcell i opened collectionview, also i need dynamic height of collectionview according to its item.also tableviewcell height dynamic. currently i am statically chaing height based on expand collpase.I need similar to this image
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.selectedIndex == indexPath.row) {
return 150;
}else{
return 40;
}
}
tableview method for adjusting row height, i want it to be dynamically set height. also i have collectionview inside it. for reference i have 2 images it working fine but with static heigt i want it dynamically.[following is the image for reference][2]
[1]: https://i.stack.imgur.com/iJ8Df.png
image when collectionview item is more set, i want it to set dynamically
First,get the cell's model,then calculate with image count,for example:
// cell height is 40
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return model.images.count / 6 * 40
}

How to get the padding from the edge of the UITableview to the UITableViewCell

On the iPad, the Grouped style tableview's cells are inset deeper from the edge of the tableview than on the iPhone.
I need to retrieve the Left and Right distances from the edges of the tableview to where the cell begins. What i'm referring to is similar to "Margins". I read the UITableview API up and down and can't find a property that returns this.
I need to use this in calculation to compute where to position content in my cells.
Thanks in advance!
Alex
I haven't tested this but i'm pretty sure you should just be able to pick up the frame of both and then compare from there.
CGRect cellFrame = yourCell.frame;
CGRect tableFrame = yourUITableView.frame;
The CGRect values are (x coordinate, y coordinate, width, height).
Also you can just print out the frames using :
NSLog(#"your cell frame is %#",NSStringFromCGRect(yourCell.frame);
NSLog(#"your table frame is %#",NSStringFromCGRect(yourUITableView.frame);
I solved this with overriding the layoutSubviews call for the iPad and setting the grouped view margins to what I want them to be, rather then what the apparently hidden value is. Other answers in Stack point out that it can vary from 10 to 45 pixels in width.
In your CustomTableViewCell class
- (void)layoutSubviews
{
CGRect f = self.bounds;
f = CGRectInset(f, 10, 0);
self.contentView.frame = f;
self.backgroundView.frame = f;
}
You could force it to keep contentView and backgroundView to be equal to that of the TableCell width which is that TableView width, but in this case I still wanted my grouped view to be inset a little bit. It also allows you to better match with a custom header/footer view which will go edge to edge without work.

UITextView size for UITableViewCell

I have a tableview, one of the rows contains a cell that contains a UITextView. I need to know the size of the textview because I need it to fit the cell and return that size in 'heightForRowAtIndexPath' method. Using the NSString method for size only works for labels, not for textviews. What are my options?
Thanks
Does the UITextView already have the proper size, or do you need to resize it as well to exactly contain its text? There are no out-of-the-box methods to do that I believe.
If it already has the proper size, you can just get the height from its frame:
CGRect frame = textView.frame;
CGSize size = frame.size;
CGFloat = size.height;

Variable height NSTextField with PXListView

I am trying to use PXListView to show a table of cells containing NSTextFields and I want to vary the height of each cell to fix the text within it.
On my subclassed cell view I have a height method that calculates the height the cell should be:
- (CGFloat)height{
NSRect textRect = [self.text.attributedStringValue
boundingRectWithSize:NSMakeSize(NSWidth(self.text.frame), 0.0f)
options:NSStringDrawingUsesLineFragmentOrigin];
return MAX(90.0f, NSHeight(textRect)+73.0f);
}
Then on my PXListViewDelegate's heightOfRow method I get the cell for the row then get the height of that cell:
- (CGFloat)listView:(PXListView *)listView heightOfRow:(NSUInteger)row{
CustomViewCell *cell = (CustomViewCell *)[listView cellForRowAtIndex:row];
return [cell height];
}
The problem is that cell is always null, I believe this is because I have a chicken or the egg scenario. The cell is null because cellForRow is never called because the height is zero so no cell need to be loaded.
What is the correct way to vary the height of a PXListView cell based on the text within it?
My solution was to use the width of the table view to calculate the height of each table cell:
- (CGFloat)listView:(PXListView *)listView heightOfRow:(NSUInteger)row{
NSRect textRect = [self.text.attributedStringValue
boundingRectWithSize:NSMakeSize(NSWidth(self.tableView.frame), 0.0f)
options:NSStringDrawingUsesLineFragmentOrigin];
return MAX(90.0f, NSHeight(textRect)+73.0f);
}

UITableView row height not drawing taller with text

I'm trying to mimic the address cell in Address Book / Contacts App.
I want a cell with style UITableViewCellStyleValue2 to display an address such as:
// 123 Fake St.
// Suite 555
// Denver CO, 80000
But actual cell isn't drawing the way I want. The label is drawing outside the bounds of the tableview row and part of top of the label is being cut off by the row directly above it.
Here's what I have so far:
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.autoresizingMask = UIViewAutoresizingFlexibleHeight; //doesn't seem to do anything
self.tableView.rowHeight = 88.0f; // default is 44.0
cell.detailTextLabel.numberOfLines = 0; //unlimited lines
An NSLog statement confirmed the rowHeight is set to 88.0.
I realize a delegate method exists to change the row height. However, in my current app I only need this one cell to expand with the text, so I didn't feel it necessary to implement the delegate method.
Thank you for your help.
I don't think that setting row height this way (self.tableView.rowHeight = 88.0f;) for a single row will work. You should implement the delegate method which in your case is very simple, i.e.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return ([indexPath row] == MY_TALLER_CELL_ROW) ? 88.0f : 44.0f;
}
I think you'll probably have more luck not using the cell's default textLabel and detailTextLabel views; instead, create your own labels and add them to the cell's content view.