I need to create expandable-cell form nib. It looks fine if dimenssions of cell in my tableview are equal to the dimensions of custom-cell nib. But when i'm trying to change height of custom cell to make it smaller it looks like this:
Can you give me some advices how can i make expand/collapse custom table cell from nib and avoid this effect?
UPD: I have heightForRowAtIndexPath implemented and it works fine. The base problem that all outlets that should be visible only when the cell is expanded are visible over all tableview for all cells when there are collapsed
I found the solution and it's very simple. I just need to set "Clip SubViews" property of my custom-cell in the Interface Builder.
Look at the heightForRowAtIndexPath datasourse method. It can be looking like this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (expandableCell)
return 20.0f;
return 44.0f;
}
Related
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.
Is it possible to use this feature without using text related views? This would be to not have to calculate the height, but instead let the cell self-size using subviews with constant height constraints, and eventually changing those height constraints at runtime.
The proper way to handle dynamic cell height changes in a table view is through tableView:heightForRowAtIndexPath: in UITableViewDelegate. You can grab the height from the subviews but you have to link that value to the index path of the cell.
Yes,you could build a tableviewcell with dynamic height.
1.use constraints to configure your tableviewcell's subviews
2.add a line of code In viewdidload method:
self.tableView.rowHeight = UITableViewAutomaticDimension
3.in some cases you must add another line of code In viewdidload method:
self.tableView.estimatedRowHeight = 40 //or any value other than your tableviewcell's height in IB.
Yes you can do that.
First Add constraints to subviews and add the following code in .m file.
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
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.