IOS - viewForFooterInSection attached to the bottom of a UITableView

I have a UITableView with three sections. Each one has a footer that I added using viewForFooterInSection. The problem I am facing is that when I scroll down the table, the footer fits into the bottom of the screen and does not scroll like the rest of the cells. Does anyone know how to make the footer almost act like a cell and scroll along with the rest of the table? Thanks!

+6
source share
5 answers

I really understood that. This is probably not the smartest way to do this, but I changed the style of the UITableView to a grouped one, and that fixed it. I had to tweak the TableView a bit to make the cells look the same as ungrouped ones (transparent background, no separators), but it worked fine. The footer no longer adheres to the bottom of the TableView.

+10
source

The footers must adhere. The trick is to add an extra cell to each section and render the footer. If you need more help, add a comment, but it should be pretty simple.

edits:

Q: Good. I am using Core Data and NSFetchedResultsController to populate a TableView. Would it be more difficult to do this?

A: Not at all. Override

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

To add an extra cell to each section and to

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

check if indexPath.row> than your fetchedResultsController strings for this section. If true, add footer information to your cell.

+5
source

One way: set the footer as one of the cells as the last cell in the scroll view (you could do it, but set it as the last element in the array from which you selected the appropriate one)

+4
source

I had a similar problem when I found out that my code doesn’t work with landscape mode and only works in Portrait mode. In my old code, when in the landscape the table view cannot scroll below the visible view, it returns to the top row when scrolling (not allowing me to see the bottom rows). All I have to change is to make sure the height is set to 44 by default. So my footer is basically another cell with clearColor. Note that my application uses "AutoLayout"

 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { CGRect screenBounds = [UIScreen mainScreen].bounds; CGFloat width = screenBounds.size.width; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 44)]; view.backgroundColor = [UIColor clearColor]; UILabel *label = [[UILabel alloc] initWithFrame:view.frame]; label.text = @"Your Text"; [view addSubview:label]; return view; } 
+1
source

Adding an extra cell, making it invisible, and rendering your view is not an advisable way to add a footer. Doing it right is pretty straight forward:

 - (UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section { NSString* sectionFooter = [self tableView:tableView titleForFooterInSection:section]; UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, yourWidth, yourHeight)]; //create a view- the width should usually be the width of the screen UILabel* label = [[UILabel alloc] initWithFrame:someFrame]; label.backgroundColor = [UIColor blueColor]; label.textColor = [UIColor whiteColor]; label.text = sectionFooter; [view addSubview:label]; return view; } 

You will also need to implement tableView: titleForFooterInSection: if you want to add text, as I am here.

0
source

Source: https://habr.com/ru/post/920525/


All Articles