My table view has 8 sections and I set the height for footer of the last section (section 7) is 80 so that the view of the table is friendly to user.
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
switch (section) {
case 7:
return 80;
}
return 10;
}
My cell has a text field and i program so that when a keyboard pops up, I still can scroll the
table and see every section. When the keyboard is dismissed, I notice that the height for footer of the last section has been shrinking a bit.
My question : how can I force me table to redraw the height for footer of sections after dismissing the keyboard
you can call reloadData, methode of UITableView
Related
This question already has answers here:
tableFooterView property doesn't fix the footer at the bottom of the table view
(10 answers)
Closed 8 years ago.
I have a UITableView with a footer, filled with a tabBar in a custom view, done using the following code:
- (CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section {
//differ between your sections or if you
//have only on section return a static value
return 49;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if(footerView == nil) {
//allocate the view if it doesn't exist yet
footerView = [[UIView alloc] init];
[footerView addSubview:self.tabBarView];
}
//return the view for the footer
return footerView;
}
Which is working lovely, apart from when the table has less rows than are needed to fill the screen, this causes the footer to move up the screen, as the table no longer creates empty rows, due to having a footer.
So, does anyone know of a way to either lock the custom footer to the bottom of the screen, or, to make the tableView create empty rows as it used to do?
Thanks!
Gareth
Unfortunately I don't think there is an easy way to do this, other than some view hierarchy trickery. When the contentSize of your UITableView is less than the frame size, you assign the footer view to self.view and position manually. When the contentSize of your UITableView is greater than the frame size, you use viewForFooterInSection. Let me know if this isn't clear or if you'd like to see some sample code on how to do this.
I have the same problem for uitableview controller, I solved this by adding view in the window and remove this object in viewWillDisappear.
My Solution link
I have a UICollectionView that contains some large UICollectionViewCells. These cells are so large that they completely fill the UICollectionView bounds and extend off screen.
The problem is that the large cells are removed from the UICollectionView and queued for reuse while they are still displayed, resulting in a blank view. If I continue scrolling on the blank UICollectionView area, eventually the final portion of the cell appears and the start of the next cell appears in exactly the right place.
I've effectively disabled cell reuse and the problem still occurs apparently because the UICollectionView thinks that the cell is no longer displayed since no corner is within the bounds of the collection view.
To demonstrate make a collection view that is a single column and have a cell that is 10000px tall, when scrolling over the very tall cell it will disappear after about 1000px of content is scrolled off the screen and will reappear to display the final 1000px of content for the cell.
You can download a simple prototype app that displays this problem at: http://jack.cox.s3.amazonaws.com/collectionviewprototype.zip
This issue happened for me along with this warning in the debug log:
the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less that the height of the UICollectionView
minus the section insets top and bottom values.
It seems that the out of the box UICollectionViewFlowLayout does not support cells larger than the screen.
This issue is being tracked as radar #12889164
Try this fix if you are still looking for a quick solution:
When a cell is detected to have a bigger height than the collectionview holding it, the collection view simply needs to be larger than the cell. So set the collecitonView frame to be bigger and correct the content and indicator insets.
- (void)updateCollectionViewForLargeCellHeight:(CGFloat)largeCellHeight {
CGFloat currentCollectionViewHeight = CGRectGetHeight(self.collectionView.frame);
if (largeCellHeight > currentCollectionViewHeight) {
self.collectionViewBottomConstraint = -largeCellHeight;
//This is the bottom constraint of the collectionView to its superview.
//If you are not using constraints, simply set the frame for the collectionView
UIEdgeInsets edgeInset = UIEdgeInsetsMake(0, 0, largeCellHeight, 0);
self.collectionView.contentInset = edgeInset;
self.collectionView.scrollIndicatorInsets = edgeInset;
[self.collectionView needsUpdateConstraints];
}
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize size;
[self updateCollectionViewForLargeCellHeight:size.height];
return size;
}
I have used UICollectionView with cells of the size of the screen. For iPad the cell size was 768x1024. And haven't found any issues. Please make sure that you have set the Min Spacing For Cells and For Lines are 0 Also return correct CGSize and UIEdgeInsets as follows
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize retval;
retval.width=maxWidth; //768 in case of iPad
retval.height=maxHeight; //1024 in case of iPad
return retval;
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0 , 0, 0, 0);
}
Im using a tableview to display some information in a quiz app that Im working on. My question is how do i make the tableview only show the number of cells that I need. Ive set the number of rows delegate method like this:
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
but at the bottom of the table view are empty cells that are not needed. If I set the tableview style to grouped I get 5 cells and no empty ones below them. Ive seen that other people have done this but cant seem to work it out. I was wondering if they have somehow added a custom view to the table footer to cancel the empty cells out?
Any ideas or help appreciated.
If you do want to keep the separator, you could insert a dummy footer view. This will limit the tableview to only show the amount of cells you returned in tableView:numberOfRowsInSection:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
In swift:
self.tableView.tableFooterView = UIView(frame: CGRectZero)
A much nicer method which doesn't require cell resizing is to turn off the default separator (set the style to none) and then have a separator line in the cell itself.
I was having a similar problem, how to show only separators for the cells that contain data.
What I did was the following:
Disable separators for the whole tableView. You can do that in the
inspector for the tableview in Interface builder or by calling
[yourTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];.
Inside your cellForRowAtIndexPath where you populate your tableview with cells create a new UIView and set it as a subview to the cell. Have the background of this view lightgray and slightly transparent. You can do that with the following:
UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake:
(0, cell.frame.size.height-1,
cell.frame.size.width, 1)];
[separatorView setBackgroundColor:[UIColor lightGrayColor]];
[separatorView setAlpha:0.8f];
[cell addSubView:separatorView];
The width of this view is 1 pixel which is the same as the default separator, it runs the length of the cell, at the bottom.
Since cellForRowAtIndexPath is only called as often as you have specified in numberOfRowsInSection these subviews will only be created for the cells that possess data and should have a separator.
Hope this helps.
This worked for me - I had extra empty rows at the bottom of the screen on an iphone 5 -
In my case I needed 9 rows
- (CGFloat)tableView:(UITableView *)tabelView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.tableView.frame.size.height / 9;
}
You can implement heightForRowAtIndexPath: and compute the correct height to only show 5 cells on the screen.
Are you always going to have 5 rows? If it's a dynamic situation you should set the number of rows according to the datasource of the tableview. For example:
return [postListData count];
This returns the count of the records in the array holding the content.
The tableview is only going to display the number of rows and sections that you tell it to. If you're always going to have just a single section, DON'T implement the method below.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
Without this the tableview will only have 1 section. With it, as you would imagine, you can specify the number of sections.
It is quite Simple. Just set the size of the popover like this:
self.optionPickerPopOver.popoverContentSize = CGSizeMake(200, 200);
Certainly you can adjust the size (200,200) depending upon the size of contents and number if rows.
Easy way would be to shrink tableView size. I.e. 5 cells 20 points each gives 100.0f, setting height to 100.0f will cause only 5 rows will be visible. Another way would be to return more rows, but rows 6,7 and so would be some views with alpha 0, but that seems cumbersome. Have you tried to return some clerColor view as footerView?
I think u can try changing the frame of the table view, if you want to adjust with the number of cells.
Try something like this:
[table setFrame:CGRectMake(x, y, width, height*[list count])];
height refers to height of the cell
As Nyx0uf said, limiting the size of the cell can accomplish this. For example:
- (CGFloat)tableView:(UITableView *)tabelView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat result;
result = 100;
return result;
}
implement these two methods in your UITableViewController:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (section == tableView.numberOfSections - 1) {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == tableView.numberOfSections - 1) {
return 1;
}
return 0;
}
In fact, these codes are telling tableview that you don't need to render the seperator line for me anymore, so that it looks the empty cells won't be displayed(in fact , the empty cell can not be selected too)
i am using grouped table view with single section (since i want cells in rounded rect) i want to remove table view section header and footer to save that size but its header and footer are removed but same size it maintaining...... how to solve this...?
you can edit size of sections header and footer in IB. Open Inspector on your table view and go to the size tab. Set there section header/footer height to minimum
Add this code, it will resize the header and footer to zero-height:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0;
}
You can also try to move the Insets in the Inspector on your Table View or do it programatically. Beware that this acts different depending on the iOS system version the device is on, ie iOS 6 insets are different than iOS7 insets.
Doing it programmatically:
UIEdgeInsets inset = UIEdgeInsetsMake(-35, 0, 0, 0); //iOS7 insets to line up with the navigation bar
self.tableView.contentInset = inset;
self.tableView.scrollIndicatorInsets = inset; //This is to make sure the scroll indicators are also the same.
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.