I'm using iOS 9.2 and XCode 7.2.
I have a basic UITableView object in which i add different kind of UITableViewCell subclasses.
With one of them, when i set manually the height overriding the method heightForRowAtIndexPath, i don't get any content on the cell.
If i return -1 as height(the default value for the UITable row height), i get my cell showing up correctly. The thing is that i do need a different height for this row because the content is quite big.
here is the code for heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
MenuItem *menuItem = [menuManager menuItemAtIndex:indexPath.row];
if ([menuItem type] == MenuItemTypeEditorHeader) {
return 100;
} else {
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
}
MenuItem is a class containing the specific menu object' informations, such as the type. The result is that the cell is showed up at the correct height, but it's empty.
Its not advisable to use heightForRowAtIndexPath anymore - thats old-school. Instead, do this :
Set up autolayout constraints in your cell (if you dont know how to - you need to, its not something you can avoid anymore!)
Create an estimatedRowHeight for autolayout to use, on the tableView. You can set it in the nib/storyboard or programmatically, in viewDidLoad for eg, like this :
self.tableview.estimatedRowHeight = 68.0;
Set your tableview to use 'automatic dimension', like this :
self.tableview.rowHeight = UITableViewAutomaticDimension;
And thats it. If you do these things, then your cells will vary in height according to their constraints. So if one of your subclasses has a height of 150px due to its constraints, that will work perfectly next to another subclass that has a height of 50px. You can also vary the height of a cell dynamically depending on the contents of the cell, for eg when you have labels that expand using 'greater than or equal to' constraints. Also - simply omit the 'heightForRowAtIndexPath' method, you dont need to implement it at all.
Are you calling tableView.reloadData() ?
print the length of menu objects before you call tableView.reloadData().
HeightForRowAtIndexPath just returns height of a row. So may be problem in cellForRowAtIndexPath.
Related
I might be wanting a strange thing, but the task is to stretch last cell in UITableView to fill the rest space. So in case table will only have few cells, then the last one will match it's height to the rest until screen bottom.
To achieve this, I included height constraint in the last cell's xib.
I can calculate proper height and set the constant, but it does not matter what I do afterwards - setNeedsDisplay(), setNeedsLayout(), reloadData(), reloadRowsAtIndexPaths() etc. - the cell height is not changed.
Only when I call the update code second time, layout is adjusted.
Any hints how to force update the cell immediately?
You need to have the following implemented (viewDidLoad is fine).
var estimatedRowHeight = 80 (for example)
// Set automatic dimension on row height.
tableView.rowHeight = UITableViewAutomaticDimension
// Set estimated row height.
tableView.estimatedRowHeight = estimatedRowHeight
Remove the height constraint and then call your resize with either the reloadData, or reloadRowsAtIndexPaths().
Did you try the following method? Where you check for the last indexPath.row and put a different height that you calculate.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
I want to know if I have customized my own tableviewcell with a specified height and this value is not the same as I used in heightForRowAtIndexPath. Which height will be used for the cell. Since I have multiple customized cells in one tableview, it will be difficult for me to distinguish each type of them in the heightForRowAtIndexPath function. I just want to know in this case, can I just use the cell height I defined for each type of cell?
You can implement this method of the UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//The height you desire to have for the row at *indexPath*
}
According to the UITableViewDelegate protocol reference,
The method allows the delegate to specify rows with varying heights. If this method is implemented, the value it returns overrides the value specified for the rowHeight property of UITableView for the given row.
My found is that before iOS8, you have to specify the row height in heightForRowAtIndexPath. Until iOS8, Apple provides autolayout and resize of cell, e.g. adding constraints.
Not sure if this is the only way to control the cell row height or not.
In my custom UITableViewCell I set the height of row as
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}
As a result (I believe), when data is rendered, the last cell is not rendered correctly.
This happens on all views where I set the heightForRowAtIndexPath. How do I fix it, so that I can see last cell as well?
You are returning the height of the cell correctly and this issue seems to be related with table view and not with the cell, I think you need to reduce the height of the tableView.
If you are using AutoLayout then you need to set constraints to your tableView with Respect to its superview.
Else if just set
[tableView setAutoresizingMask:UIViewAutoresizingMaskFelxibleHeight | UIViewAutoResizingMaskFlexibleWidth];
Perhaps your UITableView's height is not right, check it.
I'm using Apple's DateCell sample Xcode project to figure out how to use a UIPickerView inside of a UITableViewCell, but I'm having some trouble figuring out the constraints that the sample code has set up for the UIDatePicker in the storyboard.
Link: https://developer.apple.com/library/ios/samplecode/DateCell/Introduction/Intro.html
It says that the UIDatePicker has a constraint relative to the actual UITableViewCell, but when I try to set up a constraint between the two, I can't. Ctrl-dragging from the picker to the cell doesn't highlight the cell. I tried doing it with the cell's content view rather than the cell itself, but that doesn't quite produce the same result as in the sample code's storyboard.
These are the constraints set up by the project for the date picker:
And for the cell:
What the sample's storyboard looks like:
How can I reproduce the above image using constraints?
Any help would be greatly appreciated.
I found that the sample's static height was not what I wanted to use anyway, since I have some cells with lots of text that need to have variable heights for each cell. So instead of the approach in the sample which grabs the height of the picker cell from the pre-defined storyboard size, I used UITableViewAutomaticDimension to automatically fit all of my cells for height. If you don't want auto height, it's still pretty easy to adapt the following solution to set the height to a static value, as I mention in step 3.
Set tableview row height to auto when the view loads:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.tableView.estimatedRowHeight = 60.0; // Set your average height here.
self.tableView.rowHeight = UITableViewAutomaticDimension;
}
Add an estimated height so that it works properly (at least in iOS 8). If you don't add this, you might get some buggy behavior when it draws the cell heights.
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
For auto height, remember to remove the tableView:heightForRowAtIndexPath: definition if you have one, as it could interfere with the auto settings.
// Don't define this at all if you want the picker cell to have auto height.
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{ }
If you want to use a static height for the picker cell, then define the tableView:heightForRowAtIndexPath: function and just return the static value here instead of using the auto dimension like the sample does it. Example would be something like this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// This would return the static height for the picker, but auto heights for the rest of the cells.
return ([self indexPathHasPicker:indexPath] ? self.pickerCellRowHeight : UITableViewCellAutoDimension);
}
And then you'd modify the sample to set your static height instead of grabbing it from the frame value in viewDidLoad.
// obtain the picker view cell's height, works because the cell was pre-defined in our storyboard
//UITableViewCell *pickerViewCellToCheck = [self.tableView dequeueReusableCellWithIdentifier:kDatePickerID];
self.pickerCellRowHeight = 216;//CGRectGetHeight(pickerViewCellToCheck.frame);
Rather than trying to recreate the same constraints from the sample, I told storyboard to Reset to Suggested Constraints for the UIPickerView, and then added one more. It added the first two in the picture, and then I just added one more for aligning the center X. My view happened to have the shown numbers from the auto settings but yours may be different.
In case you haven't already done it, part of making the picker work is to make sure the dataSource and delegate for the UIPickerView are set to the view controller using storyboard and control clicking and dragging.
I have a tableview that fill with custom cells,, Now I want to increase the width of the cell,I increased the cell height of UITableview. But the problem is still same. Any one know how to increase the height of custom cell in UITableView.
Thanks,
If your cell heights vary, implement the following delegate:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44.0; // edit this return value to your liking
}
If they don't, merely set the rowHeight property of your table view instance to whatever height you want all of your cells to be. This answer does a good job of explaining why.