Add loading footer on UITableView - ios

I have a UITableView. When I scroll to the bottom it loads more data. How can I add a loading animation to the footer of the table when it´s scrolled to the bottom?

You can use MNMBottomPullToRefreshManager file to load more data. First you have to initialize it in viewDidLoad as
pullToRefreshManager_ = [[MNMBottomPullToRefreshManager alloc] initWithPullToRefreshViewHeight:60.0f tableView:table withClient:self];
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[pullToRefreshManager_ tableViewScrolled];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate: (BOOL)decelerate
{
[pullToRefreshManager_ tableViewReleased];
}
- (void)bottomPullToRefreshTriggered:(MNMBottomPullToRefreshManager *)manager
{
//method to get more data
// [self CallWebServiceToLoadMorePAYMENTS];
}
}
After reloading your tableView call:
[pullToRefreshManager_ tableViewReloadFinished];

Try this
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *headerView = [[[UIView alloc] init]autorelease];
[headerView setBackgroundColor:[UIColor clearColor]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Load More" forState:UIControlStateNormal];
button.frame = CGRectMake(10.0, 210.0, 160.0, 40.0);
[headerView addSubview:button];
return headerView;
}

Use this:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if(array.count>1){
if(indexPath.row == array.count-1){
[self setupTableViewFooter];
}
}
}
- (void)setupTableViewFooter
{
// set up label
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
footerView.backgroundColor = [UIColor clearColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.font = [UIFont fontWithName:Font_MuseoSans size:14];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.textColor = [UIColor darkGrayColor];
label.text = #"Loading";
self.footerLabel = label;
CGSize labelSize = [self.footerLabel sizeThatFits:footerView.frame.size];
[footerView addSubview:self.footerLabel];
// set up activity indicator
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicatorView.hidesWhenStopped = YES;
activityIndicatorView.color = [UIColor blackColor];
self.activityIndicator = activityIndicatorView;
[self.activityIndicator startAnimating];
[footerView addSubview:self.activityIndicator];
CGRect footerFrame = footerView.frame;
label.frame = CGRectMake((footerFrame.size.width-labelSize.width - 4 - activityIndicatorView.frame.size.width)/2, (footerFrame.size.height-labelSize.height)/2
, (footerFrame.size.width-labelSize.width - 4 - activityIndicatorView.frame.size.width), labelSize.height);
self.activityIndicator.frame = CGRectMake(label.frame.origin.x + labelSize.width + 4, (footerFrame.size.height-activityIndicatorView.frame.size.height)/2
, activityIndicatorView.frame.size.width, activityIndicatorView.frame.size.height);
self.tableView.tableFooterView = footerView;
}

Related

How to create UITableView programmatically

I have the following code to create a view with a table programmatically
- (void) createView:(UIViewController*)viewController{
_thisViewController = viewController;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = #"Title";
titleLabel.frame = CGRectMake(75, 20, 200, 50);
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = [titleLabel.font fontWithSize: 24];
UILabel *subtitleLabel = [[UILabel alloc] init];
subtitleLabel.text = #"Subtitle";
subtitleLabel.frame = CGRectMake(75, 50, 400, 50);
subtitleLabel.textColor = [UIColor blackColor];
subtitleLabel.font = [subtitleLabel.font fontWithSize: 20];
UILabel *labelStatus = [[UILabel alloc] init];
labelStatus.text = #"";
labelStatus.frame = CGRectMake(75, screenHeight - 290, 500, 50);
labelStatus.textColor = [UIColor blueColor];
labelStatus.font = [labelStatus.font fontWithSize: 20];
UITableView *tableDeviceList = [[UITableView alloc] init];
tableDeviceList.frame = CGRectMake(75, 100, screenWidth -250, screenHeight - 400);
tableDeviceList.dataSource = self;
tableDeviceList.delegate = self;
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
[cancelButton setTitle:#"Cancelar" forState:UIControlStateNormal];
[cancelButton sizeToFit];
cancelButton.frame = CGRectMake((screenWidth/2)-100, screenHeight - 250, 100, 50);
cancelButton.backgroundColor = UIColorFromRGB(0x066D8B);
cancelButton.layer.cornerRadius = 20;
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancelButton addTarget:self action:#selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIViewController *viewControllerModal = [[UIViewController alloc] init];
UIView *nooView = [[UIView alloc] initWithFrame:CGRectMake(50, 80, screenWidth - 100, screenHeight - 160)];
nooView.backgroundColor = UIColorFromRGB(0xE8E8E8);
nooView.layer.cornerRadius = 20;
[nooView addSubview:titleLabel];
[nooView addSubview:subtitleLabel];
[nooView addSubview:tableDeviceList];
[nooView addSubview:labelStatus];
[nooView addSubview:cancelButton];
[viewControllerModal.view addSubview:nooView];
[viewController presentViewController: viewControllerModal animated:YES completion:nil];
}
Which is throwing me an error of tableView:numberOfRowsInSection:, so I wonder if is there a way to add a function to a UITableView created this way (I'm creating my UITableView programmatically).
Also, my class is of type NSObject, so I wonder if there will be a problem with the assignment of the dataSource and delegate properties of the tableDeviceList.
You need to implement the dataSource and delegate methods of UITableView in your class,
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1; // number of sections
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10; // should be your array count
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.textlabel.text = #"test";
return cell;
}
Firstly, you should set the following:
UITableView(your tableview name).delegate = self;
UITableView(your tableview name).dataSource = self;
Secondly, you need to implement the delegate methods and datasource methods
such as:

UITableView's footer is not visible while scrolling

I have a scenario where my Table holds list of products. This list has a footerView with a button added to the footerView. I could able to see/scroll complete footerView along with the button when I open the already created Product List. But I cannot see complete button in the footerView when I create & Save the Product list for the first time.
I am using below code:
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE_WIDTH, 44)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10, 0, SCREEN_SIZE_WIDTH - 20, 44);
[footerView addSubview:button];
self.tableView.tableFooterView = footerView;
[self.tableView setTableFooterView:footerView];//Tried this one too
Please help me to show/scroll the button in footerView. Thanks!
Use this code, its working fine for me:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if(array.count>1){
if(indexPath.row == array.count-1){
[self setupTableViewFooter];
}
}
}
- (void)setupTableViewFooter
{
// set up label
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
footerView.backgroundColor = [UIColor clearColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.font = [UIFont fontWithName:Font_MuseoSans size:14];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.textColor = [UIColor darkGrayColor];
label.text = #"Loading";
self.footerLabel = label;
CGSize labelSize = [self.footerLabel sizeThatFits:footerView.frame.size];
[footerView addSubview:self.footerLabel];
// set up activity indicator
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicatorView.hidesWhenStopped = YES;
activityIndicatorView.color = [UIColor blackColor];
self.activityIndicator = activityIndicatorView;
[self.activityIndicator startAnimating];
[footerView addSubview:self.activityIndicator];
CGRect footerFrame = footerView.frame;
label.frame = CGRectMake((footerFrame.size.width-labelSize.width - 4 - activityIndicatorView.frame.size.width)/2, (footerFrame.size.height-labelSize.height)/2
, (footerFrame.size.width-labelSize.width - 4 - activityIndicatorView.frame.size.width), labelSize.height);
self.activityIndicator.frame = CGRectMake(label.frame.origin.x + labelSize.width + 4, (footerFrame.size.height-activityIndicatorView.frame.size.height)/2
, activityIndicatorView.frame.size.width, activityIndicatorView.frame.size.height);
self.tableView.tableFooterView = footerView;
}

UiTableViewHeader - Dynamic button event handler

I need to add a uiButton to a static uitableview section header - I've made an attempt with the following -
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// you can get the title you statically defined in the storyboard
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
CGRect frame = tableView.bounds;
// create and return a custom view
#define LABEL_PADDING 10.0f
HeaderLabelStyling *customLabel = [[HeaderLabelStyling alloc] initWithFrame:CGRectInset(frame, LABEL_PADDING, 0)] ;
UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-60, 10, 50, 30)];
addButton.backgroundColor = [UIColor whiteColor];
addButton.titleLabel.textColor = [UIColor colorWithRed:240/255.0f green:118/255.0f blue:34/255.0f alpha:1.0f];
addButton.titleLabel.tintColor = [UIColor blackColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
title.text = #"iawn";
customLabel.text = sectionTitle;
customLabel.backgroundColor= [UIColor colorWithRed:143.0f/255.0f green:137.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
customLabel.textColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f];
[customLabel addSubview:addButton];
[addButton addSubview:title];
[addButton addTarget:self action:#selector(receiverButtonClicked:) forControlEvents:UIControlEventTouchDown];
return customLabel;
}
-(void)receiverButtonClicked:(id)sender{
NSLog(#"button clicked");
}
the above adds a button - but doesn't react to the click event - can anyone suggest how I can get this to work?
UILabel does not handles touches by default.
Add following line of code:
customLabel.userInteractionsEnabled = YES;
In order to display button only for third section you should add following condition:
if(section == 2){...}
So your -tableView:viewForHeaderInSection: should look as follows:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection (NSInteger)section {
if(section != 2) return nil;
// you can get the title you statically defined in the storyboard
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
CGRect frame = tableView.bounds;
// create and return a custom view
#define LABEL_PADDING 10.0f
HeaderLabelStyling *customLabel = [[HeaderLabelStyling alloc] initWithFrame:CGRectInset(frame, LABEL_PADDING, 0)] ;
UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-60, 10, 50, 30)];
addButton.backgroundColor = [UIColor whiteColor];
addButton.titleLabel.textColor = [UIColor colorWithRed:240/255.0f green:118/255.0f blue:34/255.0f alpha:1.0f];
addButton.titleLabel.tintColor = [UIColor blackColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
title.text = #"iawn";
customLabel.text = sectionTitle;
customLabel.backgroundColor= [UIColor colorWithRed:143.0f/255.0f green:137.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
customLabel.userInteractionsEnabled = YES;
customLabel.textColor = [UIColor colorWithRed:255/255.0f green:255/255.0f blue:255/255.0f alpha:1.0f];
[customLabel addSubview:addButton];
[addButton addSubview:title];
[addButton addTarget:self action:#selector(receiverButtonClicked:) forControlEvents:UIControlEventTouchDown];
return customLabel;
}

Why my button in a UItableView didn't respond?

I've a UItableView with multiple section, In Each section, I've a View which contains a button, and in each of these sections one row is located but I've hided that row.
In short, my tableview is a collection of sections which in turns contains buttons on each section, when I click on the button it didn't respond but once I scroll ,my table from bottom to top the buttons are responded. What is the issue here?
Thanks!
Here is the code:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
sheettable.scrollsToTop=YES;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 22)];
CGRect rect= CGRectMake(0, 0, 320,40);
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:(UIRectCornerAllCorners) cornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame =rect;
maskLayer.path = maskPath.CGPath;
headerView.layer.mask = maskLayer;
headerView.backgroundColor=[UIColor greenColor];
UITextField *label1 = [[UITextField alloc] init] ;
label1.delegate=self;
label1.frame = CGRectMake(5, 1, 200, 35);
label1.font = [UIFont boldSystemFontOfSize:16];
label1.text = [[result2 objectAtIndex:section] objectForKey:#"cat_desc"];
UIButton *btn=[[UIButton alloc]init];
btn.frame=CGRectMake(5, 1, 200, 35);
btn.tag=section;
btn.backgroundColor=[UIColor clearColor];
[headerView addSubview:label1];
[btn addTarget:self action:#selector(selectedsections) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:btn];
cameracategory=section;
return headerView;
}
-(void)selectedsections
{
if(cameracategory==0)
{
NSLog(#"At Index");
}
if(cameracategory==1)
{
NSLog(#"At Index");
}
if(cameracategory==2)
{
NSLog(#"At Index");
}
if(cameracategory==3)
{
NSLog(#"At Index");
}
if(cameracategory==4)
{
NSLog(#"At Index");
}
}
I tested your code, it works, but I created the UITableView myself. You should check that. Also try to write more readable code :) it will be more easier in the future :)
You have some misaligned frames (subviews are bigger than parent).
- (void)sectionButtonAction:(UIButton *)sender {
NSLog(#"Section button tapped: %i", sender.tag);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
CGRect tableRect = tableView.frame;
CGRect headerRect = CGRectZero;
headerRect.size.width = tableRect.size.width;
headerRect.size.height = 22;
CGRect bezierRect = CGRectZero;
bezierRect.size.width = tableRect.size.width;
bezierRect.size.height = 44;
CAShapeLayer *maskLayer = [CAShapeLayer layer];
[maskLayer setFrame:bezierRect];
[maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bezierRect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(5, 5)].CGPath];
UIView *headerView = [[UIView alloc] initWithFrame:headerRect];
[headerView setBackgroundColor:[UIColor greenColor]];
[[headerView layer] setMask:maskLayer];
CGRect labelRect = CGRectZero;
labelRect.origin.x = 5;
labelRect.origin.y = 1;
labelRect.size.width = 200;
labelRect.size.height = 35;
UILabel *headerLabel = [[UILabel alloc] initWithFrame:labelRect];
[headerLabel setFont:[UIFont boldSystemFontOfSize:16]];
[headerLabel setText:#"asd"];
[headerView addSubview:headerLabel];
CGRect buttonRect = labelRect;
UIButton *headerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[headerButton setFrame:buttonRect];
[headerButton setTag:section];
[headerButton addTarget:self action:#selector(sectionButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:headerButton];
return headerView;
}

Header image not resizable in expandable table

I am pretty new to Xcode and this simple problem has been driving me mad! I have created an expandable table that works fine. This is some of the code on a UIView subclass for the section that expands when you tap on a cell:
- (id)initWithFrame:(CGRect)frame WithTitle: (NSString *) title Section:(NSInteger)sectionNumber delegate: (id <SectionView>) Delegate
{
self = [super initWithFrame:frame];
if (self) {
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(discButtonPressed:)];
[self addGestureRecognizer:tapGesture];
self.userInteractionEnabled = YES;
self.section = sectionNumber;
self.delegate = Delegate;
CGRect LabelFrame = CGRectMake(100, 100, 100, 100);
LabelFrame.size.width -= 100;
CGRectInset(LabelFrame, 1.0, 1.0);
//BUTTON
CGRect buttonFrame = CGRectMake(LabelFrame.size.width, 0, 100, LabelFrame.size.height);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = buttonFrame;
//[button setImage:[UIImage imageNamed:#"carat.png"] forState:UIControlStateNormal];
//[button setImage:[UIImage imageNamed:#"carat-open.png"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(discButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
self.discButton = button;
//My IMAGE
NSString *imageName = #"gradient1.png";
UIImage *myImage = [UIImage imageNamed:imageName];
UIImageView *sectionHeaderView = [[UIImageView alloc] initWithImage:myImage];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
imageView.frame = CGRectMake(20,50,100,100);
[self addSubview:sectionHeaderView];
self.headerBG = sectionHeaderView;
//HEADER LABEL
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(22, 12, sectionHeaderView.frame.size.width, 35.0)];
label.textAlignment = NSTextAlignmentLeft;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor darkGrayColor];
label.shadowOffset = CGSizeMake(0.0, -1.0);
label.text = title;
label.font = [UIFont fontWithName:#"AvenirNext-Bold" size:20.0];
sectionHeaderView.backgroundColor = [UIColor clearColor];
//label.textAlignment = UITextAlignmentLeft;
[self addSubview:label];
self.sectionTitle = label;
}
return self;
}
I have a custom image on the cell #"gradient1.png" but I don't seem to be able to resize it? Here is the header code in the UITableViewController:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection: (NSInteger)section
{
SectionInfo *array = [self.sectionInfoArray objectAtIndex:section];
if (!array.sectionView)
{
NSString *title = array.category.name;
array.sectionView = [[SectionView alloc] initWithFrame:CGRectMake(0, 10, self.tableView.bounds.size.width, 0) WithTitle:title Section:section delegate:self];
}
return array.sectionView;
}
Sorry if this is a trivial question, your help is greatly appreciated!
I don't see where you are trying to resize the image so I can't offer any help in why it is not resizing, but I found this confusing:
//My IMAGE
NSString *imageName = #"gradient1.png";
UIImage *myImage = [UIImage imageNamed:imageName];
UIImageView *sectionHeaderView = [[UIImageView alloc] initWithImage:myImage];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
imageView.frame = CGRectMake(20,50,100,100);
[self addSubview:sectionHeaderView];
self.headerBG = sectionHeaderView;
In this part of code you create two imageviews, then you resize one and add the other to the cell (I'm assuming cell) subviews. Is it possible that you are trying to resize the view that you never added as a subview of the cell?
Also, resizing should happen inside - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.
Make sure that you are resizing the proper view, ten make sure you are resizing it in the proper place.

Resources