I just implemented a Load More button into the footer of my tableView, but the footer is always scrolling with the table. The style of my tableView is UITableViewStylePlain.
Please could you tell me where I am going wrong.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footerView = nil;
if(footerView == nil) {
footerView = [[[UIView alloc] init] autorelease];
footerView.backgroundColor = [UIColor clearColor];
UIButton *button = [[UIButton alloc] init];
button.layer.cornerRadius = 7;
[button setFrame:CGRectMake(10, 3, 300, 44)];
[button setTitle:@"Load 20 more" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[button setBackgroundColor:[UIColor whiteColor]];
[button.titleLabel setTextColor:[UIColor blackColor]];
[button addTarget:self action:@selector(load20More) forControlEvents:UIControlEventTouchUpInside];
[footerView addSubview:button];
[button release];
}
if ([songInfo count] > 20 && count < 100) {
return footerView;
}
else
return nil;
}
Let's add a footer now, again, we just need to drag a new view onto the table view, but this time place it below the other view. When you place it below the other view you should see a blue line again, when you do, it will be in the correct position. See gif below:
When the user scrolls back to a row, the table view will request its data again. The tableView(_:numberOfRowsInSection:) method tells the table view how many elements a section contains. The table view uses this information to prepare its scrolling area and display a scroll bar of the appropriate size.
For example, many developers make their life harder using a scroll view when a UITableView would be a better choice. Finally, architecture is crucial for table views. The code of the table view data source often ends inside view controllers when it should go into a separate class.
Table views are more versatile than you might think. For example, many developers make their life harder using a scroll view when a UITableView would be a better choice. Finally, architecture is crucial for table views.
First of all, tableView:viewForFooterInSection
does not define a footer for the table, it defines a footer for the section in the table. You can have multiple section footers in one table. In the case where your table is only one section, using this method will be functionally equivalent to adding a table footer, but it is NOT meant to be the table's footer. It is much better practice to use the actual table footer if that's what you want, which can be accomplished by simply assigning a view to the UITableView
's tableFooterView
property.
However, table footers (section footers AND table footers) are both built to scroll with your table. If you are looking to implement a "footer" that sticks to the bottom of the screen and does not scroll with the table, your best bet is to resize your UITableView
smaller to make room for your "footer", and add a new view to sit in the spot you just cleared for it. That way, it will stick in place, and the scrollable region in your table will not overlap with the "footer".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With