Double footerview in tableview when reload section - ios

I have uitableview footer that i create programatically like this:
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
CGRect footerRect = CGRectMake(0, 0, self.view.frame.size.width, 45);
UIView *tableFooter = [[UIView alloc] initWithFrame:footerRect];
tableFooter.backgroundColor = [UIColor clearColor];
UIButton* moreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[moreButton setBackgroundImage:[UIImage imageNamed:#"moreImage"] forState:UIControlStateNormal];
[moreButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[moreButton addTarget:self action:#selector(actionClicked:)forControlEvents:UIControlEventTouchUpInside];
moreButton.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[moreButton setFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];
[tableFooter moreButton];
return tableFooter;
}
And somewhere in my code i retrieve again data from server and reload certain section using this:
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:mySection] withRowAnimation:UITableViewRowAnimationFade];
However when the table present the new data i have two footer view on mysection, one with correct position, one just floating somewhere in my section. How i remove this footer?
Thanks.

Related

I'm having custom tableview cell and I set a overall button at a bottom of a tableview

enter image description hereI'm having custom tableview cell and I want to set a overall button at a bottom of a tableview with clickable
UIButton*yourBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[yourBtn setImage:[UIImage imageNamed:#"wishlist"] forState:UIControlStateNormal];
//[yourBtn setTitle:#"+" forState:UIControlStateNormal];
yourBtn.frame = CGRectMake(0, self.view.bounds.size.height -100, buttonwidth, buttonheight);
yourBtn.center = CGPointMake(self.view.center.x, self.view.bounds.size.height -100);
[yourBtn addTarget:self action:#selector(addButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourBtn];
if i scroll tableview button also scroll from top to bottom.i want to set that button constant when scroll tableview
How to get a tableview button like this
You need to separate the tableview and the button.
Here is the code:
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:tableView];
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
yourButton.frame = CGRectMake(CGRectGetWidth(self.view.frame) - 70,
CGRectGetHeight(self.view.frame) - 70,
50,
50);
[yourButton setImage:[UIImage imageNamed:#"Untitled-1"] forState:UIControlStateNormal];
[self.view addSubview:yourButton];
I hope the above information is helpful for you.

iOS: How to add swipe to delete button to a custom view in viewForHeaderInSection

There is a expand-collapse like table view with custom header using viewForHeaderInSection.
In that, I want to add swipe gesture functionality to delete the same view i.e section header.
My code for viewForHeaderInSection is,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
mView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 110)]autorelease];
mView.backgroundColor=[UIColor whiteColor];
[mView setBackgroundColor:[UIColor whiteColor]];
UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 290, 28)];
title.text=[[updateDataArray objectAtIndex:section] objectForKey:#"message"];
title.font = [UIFont fontWithName:#"Raleway-Medium" size:18];
UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
[bt setFrame:CGRectMake(0, 0, 320, 44)];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[bt setTag:section];
addCellFlag=2;
[bt.titleLabel setFont:[UIFont systemFontOfSize:20]];
[bt.titleLabel setTextAlignment:NSTextAlignmentCenter];
[bt.titleLabel setTextColor:[UIColor blueColor]];
[bt setBackgroundColor:[UIColor whiteColor]];
[bt addTarget:self action:#selector(addCell:) forControlEvents:UIControlEventTouchUpInside];
[mView addSubview:bt];
if (section<updateDataArray.count-1) {
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 2, 320, 0)];
lineView.backgroundColor=[UIColor lightGrayColor];
[bt addSubview:lineView];
[lineView release];
}
mView.backgroundColor=[UIColor clearColor];
[mView addSubview:title];
return mView;
}
Please suggest how to create a swipe to delete button functionality like table view row? I want that button in section header as well.
Add this lines in viewForHeaderInSection before returning your view:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
....
// Your code here
....
mView.tag = section;
UISwipeGestureRecognizer* sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(headerViewSwiped:)];
[sgr setDirection:UISwipeGestureRecognizerDirectionRight]; // change direction accordingly
[mView addGestureRecognizer:sgr];
return mView;
}
- (void)headerViewSwiped:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
UIView *mView= (UIView *)gestureRecognizer.view;
// use mView.tag property to get the section index which you require to delete from array
// update your sectionDataSource array remove all the objects from section array
// so that it will reflect to numberOfSection method
// then remove all the rows which that section containing from your rowDataSource array
// so that it will reflect to numberOfRowsInSection method
// now call [tableView reloadData] method
}
}
This is a base idea to achieve what you required, change this code as per your project requirements.
Hope this helps.
Here are codes DMSLidingCell and TISwipeableTableView. And here is way how to make your own one. make-swipeable-table-view-cell-actions-without-going-nuts-scroll-views. Now if you want to implement this to your header or any other view. Just change the tableview cell to your desired view.
you can add UISwipeGestureRecognizer to every HeaderView
Try with following way
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView * viewTemp = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
[viewTemp setBackgroundColor:[UIColor redColor]];
[viewTemp setTag:(section + 100)];
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipedScreen:)];
swipeGesture.direction = (UISwipeGestureRecognizerDirectionLeft );
[viewTemp addGestureRecognizer:swipeGesture];
return viewTemp;
}
- (void)swipedScreen:(UISwipeGestureRecognizer*)gesture
{
UIView * viewTemp = (UIView *) [self.tableView viewWithTag:[gesture.view tag]];
if(viewTemp){
NSLog(#"Do your stuff");
}
}
Add yourView and deleteButton to a container view, add swipe left gesture to the yourView.
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{
UIView* mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, thisHeaderHeight)];
//your view
YourView* view;
view.frame = CGRectMake(0, 0, self.view.frame.size.width, thisHeaderHeight);
//....your code
//add button
UIButton* _customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_customButton setTitle:NSLocalizedString(#"Delete", nil) forState:UIControlStateNormal];
_customButton.frame = CGRectMake(self.view.frame.size.width - thisButtonWidth, 0, thisButtonWidth, thisHeaderHeight);
[_customButton addTarget:(id)self action:#selector(onPressDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
//add swipe gesture
UISwipeGestureRecognizer* slgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(onSwipeHeader:)];
[slgr setDirection:UISwipeGestureRecognizerDirectionLeft]; // change direction accordingly
[view addGestureRecognizer:slgr];
UISwipeGestureRecognizer* srgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(onSwipeHeader:)];
[srgr setDirection:UISwipeGestureRecognizerDirectionRight]; // change direction accordingly
[view addGestureRecognizer:srgr];
//view covers customButton
[mainView addSubview:customButton];
[mainView addSubview:view];
return mainView;
}
That's swipe gesture function.
-(void)onSwipeHeader:(UISwipeGestureRecognizer *)gr{
if (gr.state == UIGestureRecognizerStateEnded) {
UIView *mView= (UIView *)gr.view;
CGRect newFrame = mView.frame;
if(gr.direction == UISwipeGestureRecognizerDirectionLeft){
newFrame.origin.x = -thisButtonWidth;
}else if(gr.direction == UISwipeGestureRecognizerDirectionRight){
newFrame.origin.x = 0;
}
[UIView animateWithDuration:0.25 animations:^{mView.frame = newFrame;}];
}
}
Finally, write code to performance delete.

Adjusting the height of a uitableview section footer

I am just starting to use a UITableView section footer, but am having some problems. So I have a UITableView with an edit button attached to the bottom using a footer. My first problem is that when you click on that button, the footer should double in size because the edit button then becomes an add button and a done button, instead of just one button. What would be the best way to make the size of the footer bigger. I have tried changing the frame of the footer through self.tableView.tableFooterView.frame = CGRectMake(0, 0, 320, 88); but this does not work.
My second problem is that I would like the footer to always scroll with the table, like it is just another cell. It should just be located at the bottom of the table no matter what. If the user scrolls down, it should not stop at the bottom of the screen and stick, it should just keep scrolling with the rest of tableview. How would I fix this?
Here is how I am creating the footer:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 48;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
//view.backgroundColor = [UIColor grayColor];
self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.addButton.frame = CGRectMake(0, 0, 320, 44);
[self.addButton setImage:[UIImage imageNamed:#"Add_Class_Button"] forState:UIControlStateNormal];
[self.addButton setImage:[UIImage imageNamed:#"Add_Class_Button"] forState:UIControlStateHighlighted];
[self.addButton addTarget:self action:#selector(addSelected:) forControlEvents:UIControlEventTouchUpInside];
self.addButton.hidden = YES;
[view addSubview:self.addButton];
self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.editButton.frame = CGRectMake(0, 0, 320, 44);
[self.editButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateNormal];
[self.editButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateHighlighted];
[self.editButton addTarget:self action:#selector(editSelected:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:self.editButton];
self.editDoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.editDoneButton.frame = CGRectMake(0, self.addClassButton.frame.size.height-2, 320, 44);
[self.editDoneButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateNormal];
[self.editDoneButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateHighlighted];
[self.editDoneButton addTarget:self action:#selector(doneSelected) forControlEvents:UIControlEventTouchUpInside];
self.editDoneButton.hidden = YES;
[view addSubview:self.editDoneButton];
self.editLabel = [[UILabel alloc] init];
self.editLabel.frame = CGRectMake(10, 5, 100, 30);
self.editLabel.text = #"Edit";
self.editLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
[view addSubview:self.editLabel];
self.addLabel = [[UILabel alloc] init];
self.addLabel.frame = CGRectMake(10, 5, 100, 30);
self.addLabel.text = #"Add";
self.addLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
self.addLabel.hidden = YES;
[view addSubview:self.addLabel];
self.editDoneLabel = [[UILabel alloc] init];
self.editDoneLabel.frame = CGRectMake(10, self.addClassButton.frame.size.height + 5, 150, 30);
self.editDoneLabel.text = #"Done";
self.editDoneLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
self.editDoneLabel.hidden = YES;
[view addSubview:self.editDoneLabel];
self.theLine = [[UIView alloc] init];
self.theLine.frame = CGRectMake(0, 0, 320, .5);
UIColor *borderColor = [UIColor colorWithRed:193/255.5 green:193/255.0 blue:193/255.0 alpha:1.0];
self.theLine.backgroundColor = borderColor;
[view addSubview:self.theLine];
return view;
}
Edit
After the taking into account the answers bellow, this is what I have switched to, but now where my footer used to be, there is just a grey rectangle.
Here is the code that I am now using:
-(void)setUpFooter {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 48)];
view.backgroundColor = [UIColor redColor];
self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.addButton.frame = CGRectMake(0, 0, 320, 44);
[self.addButton setImage:[UIImage imageNamed:#"Add_Class_Button"] forState:UIControlStateNormal];
[self.addButton setImage:[UIImage imageNamed:#"Add_Class_Button"] forState:UIControlStateHighlighted];
[self.addButton addTarget:self action:#selector(addSelected:) forControlEvents:UIControlEventTouchUpInside];
self.addButton.hidden = YES;
[view addSubview:self.addButton];
self.editButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.editButton.frame = CGRectMake(0, 0, 320, 44);
[self.editButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateNormal];
[self.editButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateHighlighted];
[self.editButton addTarget:self action:#selector(editSelected:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:self.editButton];
self.editDoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.editDoneButton.frame = CGRectMake(0, self.addClassButton.frame.size.height-2, 320, 44);
[self.editDoneButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateNormal];
[self.editDoneButton setImage:[UIImage imageNamed:#"Edit_Classes_Button"] forState:UIControlStateHighlighted];
[self.editDoneButton addTarget:self action:#selector(doneSelected) forControlEvents:UIControlEventTouchUpInside];
self.editDoneButton.hidden = YES;
[view addSubview:self.editDoneButton];
self.editLabel = [[UILabel alloc] init];
self.editLabel.frame = CGRectMake(10, 5, 100, 30);
self.editLabel.text = #"Edit";
self.editLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
[view addSubview:self.editLabel];
self.addLabel = [[UILabel alloc] init];
self.addLabel.frame = CGRectMake(10, 5, 100, 30);
self.addLabel.text = #"Add";
self.addLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
self.addLabel.hidden = YES;
[view addSubview:self.addLabel];
self.editDoneLabel = [[UILabel alloc] init];
self.editDoneLabel.frame = CGRectMake(10, self.addClassButton.frame.size.height + 5, 150, 30);
self.editDoneLabel.text = #"Done";
self.editDoneLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:17];
self.editDoneLabel.hidden = YES;
[view addSubview:self.editDoneLabel];
self.theLine = [[UIView alloc] init];
self.theLine.frame = CGRectMake(0, 0, 320, .5);
UIColor *borderColor = [UIColor colorWithRed:193/255.5 green:193/255.0 blue:193/255.0 alpha:1.0];
self.theLine.backgroundColor = borderColor;
[view addSubview:self.theLine];
view.userInteractionEnabled = YES;
self.classTableView.tableFooterView = view;
self.classTableView.tableFooterView.userInteractionEnabled = YES;
}
hope I can help.
To set the section footer height you also have to change the returned value in the method heightForFooterInSection. To reload the height you can call [tableView reloadData] or [tableView reloadSections:...].
A table section footer always stick to the bottom of the screen but there is a different view used to place things after the table view.
The tableView.tableFooterView is a view that is located after the table view. Change this to add elements after the table view.
Cheers!
There are two types of footers in a UITableView. There is the table footer at the bottom of the table, and there is the table section footer at the bottom of each section.
It sounds like you want to use table section headers. However, your code snippet is setting the frame of the table footer, not the table section footers.
self.tableView.tableFooterView.frame = CGRectMake(0, 0, 320, 88);
If you want to adjust the table section footer height, the tableView:heightForFooterInSection: method will need to return a different value.

Create Button in Section for Footer and detect correct Click

i have an Section Footer with an Button in it, but i dont know how to detect which section footer is clicked in my TableView. I know i have to use something like "tag", but how?
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
self.footerHeartView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 300, 60)];
[self.footerHeartView addSubview:viewFotter];
UIView *viewFotter = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 300, 60)];
self.heartButton = [[UIButton alloc]initWithFrame:CGRectMake(200, 5, 70, 50)];
self.heartButton.tag = section;
self.heartButton.backgroundColor = [UIColor greenColor];
[self.heartButton addTarget:self
action:#selector(heartClick:)
forControlEvents:UIControlEventTouchUpInside];
[viewFotter addSubview:self.heartButton];
[viewFotter setBackgroundColor:[UIColor whiteColor]];
return self.footerHeartView;
}
- (void)heartClick:(UIButton*)sender {
NSLog(#"CLICK");
[[GTAppController sharedInstance].api sendLike:1 itemId:[[self.newsList objectAtIndex:???????]objectForKey:#"testId"]];
}
Thanks for your Time!
You cast sender as a button and read tag:
- (void)heartClick:(UIButton*)sender {
NSLog(#"CLICK");
UIButton* sectionButton = (UIButton*)sender;
int section = sectionButton.tag;
[[GTAppController sharedInstance].api sendLike:1 itemId:[[self.newsList objectAtIndex:???????]objectForKey:#"testId"]];
}
Hope this help.

Need to add multiple UI elements to tableheaderview of UITable in iOS

I have a UITableViewController that has a tableView. To this tableView, I would like to add both a label as well as a button but unfortunately, I am only able to add one or the other.
Here is my code that I am working with:
- (void) viewDidLoad {
[super viewDidLoad];
selectAllButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectAllButton setFrame:CGRectMake(280, 0, 25, 25)];
[selectAllButton setImage:[UIImage imageNamed:#"CheckBox1.png"] forState:UIControlStateSelected];
[selectAllButton setImage:[UIImage imageNamed:#"CheckBox2.png"] forState:UIControlStateNormal];
[selectAllButton setUserInteractionEnabled:YES];
[selectAllButton addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
selectAllLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 40)];
[selectAllLabel setTextColor:[UIColor blackColor]];
[selectAllLabel setBackgroundColor:[UIColor clearColor]];
[selectAllLabel setText:#"Select All"];
self.tableView.tableHeaderView = selectAllButton;
self.tableView.tableHeaderView = selectAllLabel;
}
When I run the above method, I am only able to see my label which tells me it is replacing the button. Is there a way for me to get both elements to appear in the tableheaderview?
Create a UIView, add your button & label to it as subviews, than make that view your self.tableview.tableHeaderView
...your code
UIView *headerView = [UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, selectAllButton.frame.size.height + selectAllLabel.frame.size.height)];
[headerView addSubview:selectAllButton];
[headerView selectAllLabel];
self.tableView.tableHeaderView = headerView;
When you init your button & label be sure to change the frame as they will be based on the UIView
Since tableHeaderView is an UIView, try to add it with addSubview:(UIView*) or create an UIView and add that as your tableHeaderView.

Resources