Is there anyway to create a UITableView with one universal footer? - iphone

I'm trying to create a UITableView with multiple sections. Each section has it's own header, but I am trying to make a universal footer for the entire table view that stays in one position...
Is this logic possible using the UITableViewDelegate methods? Or should I create a custom view and just try and add it as a subview to my table view? The footer I currently have contains a UIButton.
If anyone has some sample code that would be great.
Edit: This question is not the same as the one referenced. I am trying to make a universal footer that floats above the UITableView. The other question does not specify the location of the footer, only that a footer is desired.

read about UITableView's tableFooterView property.
And now some code: (using ARC)
UILabel *footer = [UILabel alloc] init];
footer.text = #"Some text" ;
footer.backgroundColor = [UIColor greenColor];
self.tableView.tableFooterView = footer;
Now at the bottom of entire tableview there is a UILabel that is green with some text.

I ended up creating a subview and adding my button to that view. I then made that view my "footer".
Here is the code that gave me the desired results.
- (void)viewDidLoad
{
[super viewDidLoad];
//self.tableView.delegate = self;
//self.tableView.dataSource = self;
//save current tableview, then replace view with a regular uiview
self.tableView = (UITableView*)self.view;
UIView *replacementView = [[UIView alloc] initWithFrame:self.tableView.frame];
self.view = replacementView;
[self.view addSubview:self.tableView];
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 370, 320, 45)];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.userInteractionEnabled = YES;
//the button should be as big as a table view cell
//width of the button can be set but the width of the view it is added to will always match the width of the tableView
[button setFrame:CGRectMake(60, 0, 200, 45)];
//set title, font size and font color
[button setTitle:#"Build" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//set action of the button
[button addTarget:self action:#selector(buildThenSegue)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[footerView addSubview:button];
footerView.userInteractionEnabled = YES;
[self.view addSubview:footerView];
self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
Take note that this is in a subclass of UITableViewController.
I referenced this answer to another question: https://stackoverflow.com/a/9084267/1091868

Related

Why is my UIButton not added to the custom header view of my viewForSectionHeader

I have this following code to add a button to my custom header for section.
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {
if (tableView == menuListTableView) {
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)] autorelease];
UIButton *headerButton = [UIButton buttonWithType:UIButtonTypeCustom];
[headerButton setFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)];
[headerButton setImage:[UIImage imageNamed:#"Gray_Gradient.png"] forState:UIControlStateSelected];
[headerButton addTarget:nil action:#selector(toggleOpen:) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:headerButton];
return headerView;
}
return nil;
}
Why is my button not added to the custom header view?? The sub view array of the headerview seems to be nil when checked in the debugging mode.
Did you see the header view itself? Check the height of the header view. Also check whether the image exists. If in doubt, change the button type to rounded rect and see whether it was added properly.
Oh I got the answer myself!!! I looked at the button code.. and the control state was set to UIControlStateSelected. instead of UIControlStateNormal. Its so ignorant of me. Sorry guys for the bothering!!

How to add a UIButton on top viewForHeaderInSection

How to add a button on top off viewForHeaderInSection. I like to add a button above section header which can be scrollable with tableview. Any idea how to do this ?
The - rather hackish - solution I saw in a pull-to-refresh implementation is to simply add your 'extra' view to the table view as a subview - just make sure it's positionned using a negative y offset. That offset should be equal to (well, rather -1 times) the height of your view. Code:
UIView *myViewAboveHeader = // however you create it
CGRect f = myViewAboveHeader.frame;
f.origin.y = -1 * f.size.height;
myViewAboveHeader.frame = f;
[tableView addSubview:myViewAboveHeader];
Edit: it seems you don't want a header view AND a view above it. In this case, just simply add a button on top of the view you return in your table view delegate method:
- (UIView *)tableView:(UITableView *)tv viewForHeaderInSection:(NSInteger)s
{
UIView *header = ...;
UIButton *btn = // create a button somehow;
[header addSubview:btn];
return header;
}
Use this code, placing it in viewdidload:
- (void)viewDidLoad {
UIView *headerViews = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];
UIButton *managePrivacyButton = [UIButton buttonWithType:UIButtonTypeCustom];
[managePrivacyButton setFrame:CGRectMake(0, 45, 320, 45)];
managePrivacyButton.titleLabel.textAlignment = UITextAlignmentLeft;
[managePrivacyButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
[managePrivacyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[headerViews addSubview:managePrivacyButton];
[self.tableView setTableHeaderView:headerViews];
[super viewDidLoad];
}
This method in particular takes as a return an UIView. UIButton is a subclass of UIView. Just create a new UIButton and return it.

setting the first responder view of the table but we don't know its type (cell/header/footer)

I have UITableViewController. In this controller I add a subview UIView with UITextField in it. When the UITextField get first responder got
setting the first responder view of the table but we don't know its
type (cell/header/footer)
Code as below:
#interface UserAlbumListViewController (){
NSString *newAlbumName;
UITextField *albumNameField;
}
#end
-(void)addAlbumButtonPressed:(id)sender{
self.tableView.scrollEnabled = NO;
CGRect frame = self.view.frame;
UIView *opaqueView = [[UIView alloc] initWithFrame:frame];
opaqueView.tag = 1001;
opaqueView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
UIView *controllerView = [[UIView alloc] init];
controllerView.tag = 1002;
controllerView.backgroundColor = [UIColor whiteColor];
controllerView.frame = CGRectMake(10.0f, 60.0f, frame.size.width - 20.0f, 90.0f);
[opaqueView addSubview:controllerView];
albumNameField = [[UITextField alloc] init];
albumNameField.borderStyle = UITextBorderStyleRoundedRect;
albumNameField.placeholder = NSLocalizedString(#"Album Name",nil);
albumNameField.keyboardType = UIKeyboardTypeDefault;
albumNameField.returnKeyType = UIReturnKeyDefault;
albumNameField.autocorrectionType = UITextAutocorrectionTypeNo;
albumNameField.autocapitalizationType = UITextAutocapitalizationTypeNone;
albumNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
albumNameField.clearsOnBeginEditing = NO;
albumNameField.text = #"";
albumNameField.frame = CGRectMake(10.0f , 10.0f, controllerView.frame.size.width - 20.0f, 30.0f);
[albumNameField becomeFirstResponder];
[controllerView addSubview:albumNameField];
UIButton *_cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_cancelButton.backgroundColor = [UIColor clearColor];
[_cancelButton setTitle:NSLocalizedString(#"Cancel",nil) forState:UIControlStateNormal];
_cancelButton.frame = CGRectMake( controllerView.frame.size.width - 90.0f, albumNameField.frame.origin.y + albumNameField.frame.size.height + 10.0f, 80.0f, 30.0f);
[_cancelButton addTarget:self action:#selector(opaqueViewCancelButtonClicked:) forControlEvents:UIControlEventTouchDown];
[controllerView addSubview:_cancelButton];
UIButton *_OKButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_OKButton.backgroundColor = [UIColor clearColor];
[_OKButton setTitle:NSLocalizedString(#"OK",nil) forState:UIControlStateNormal];
_OKButton.frame = CGRectMake( _cancelButton.frame.origin.x - 90.f, _cancelButton.frame.origin.y, 80.0f, 30.0f);
[_OKButton addTarget:self action:#selector(opaqueViewOKButtonClicked:) forControlEvents:UIControlEventTouchDown];
[controllerView addSubview:_OKButton];
[self.view addSubview:opaqueView];
[controllerView release];
[opaqueView release];
}
How to avoid this warning?
The warning is telling you that you added opaqueView to the table view directly. Table views expect interactive subviews to be added to tableHeaderView, tableFooterView, or within a cell.
The offending line is [self.view addSubview:opaqueView]; because self.view is the table view.
You can fix this is one of several ways. If you are using a navigation controller, you can push a new controller with opaqueView in it. You can pop up a modal view over the table view with opaqueView in it. You can change the structure such that self.view is not a table view (which is a lot of work).
UPDATE
This answer is kind of busy, so I will post an update. I discovered that Apple uses a different solution then the ones I suggested.
Instead of [self.view addSubview:opaqueView]; which tries to add the subview to a table view, you can do [self.view.window addSubview:opaqueView]; which adds the subview directly to the table view's window. You need to keep in mind that you are using a view so the bounds and frame may be different than self.view.
I got this "(cell/header/footer)" error when a view pushed to the NavigationController was an instance of UITableView instead of UIView. Then in the the Xib editor for the class, I added a view to the canvas, and then text was ultimately added to the view through a couple more views down. The UI Builder will let you specify UITableView as the superclass of the view and place a view on the canvas, even though adding items to this view then caused the message "setting the first responder view of the table but we don't know its type (cell/header/footer)".

Adding Custom UIButton To subView

I have subview which it initiated by touch on the UIbutton in the main view.
Once the subview pops up it displays a label with the info I provided. This all works. I just need an UIbutton to show up so I can then set it up to dismiss the subview and go back to the main view. i have searched all over and have found nothing that helps.
Here is what my code in my .m file looks like:
////Button that Displays Subview with Its Label and Button
- (IBAction)showInfo:(id)sender
/////UI Subview
{UIView *mySubview = [[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 480)];
[self.view addSubview:mySubview];
mySubview.backgroundColor = [UIColor blackColor];
mySubview.alpha = .7f;
//////Label in SubView
{UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 50)];
label.text = #"Here is info";
label.textColor = [UIColor whiteColor];
label.backgroundColor= [UIColor clearColor];
[self.view addSubview:label];
////Button in Subview
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"button-info-x.png"] forState:UIControlStateNormal];
//set the position of the button
button.frame = CGRectMake(120, 252, 68, 68);
//add the button to the view
[self.view addSubview:button];
Now I just need to add an action to the UIButton to dismiss the Subview?
First Take IBOutlet of subView and add it to the main View.
IBOutlet UIView *subView;
Then, add UIButton to the SubView like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//set the position of the button
button.frame = CGRectMake(0, 0, 100, 30);
//set the button's title
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
[btnMenu addTarget:self action:#selector(your MethodName:) forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[subView addSubview:button];
Then,
-(IBAction)your MethodName:(id)sender {
}
In Above Method Remove SubView from Main View.
If you're simply looking to set an image for a UIButton instead of a title, you're looking for the - (void)setImage:(UIImage *)image forState:(UIControlState)statemethod.
You should check the UIButton reference for more info.
Add Target to the custom button
{
.....
[button addTarget:self action:#selector(your dismiss:) forControlEvents:UIControlEventTouchUpInside];
.......
}
-(IBAction)your dismiss:(id)sender
{
// write the dismissal code here
}

How to create dynamic Buttons over dynamic ImageViews while maintaining the uniqueness

I am using PhotoScroller project and want to create buttons over imageViews programmatically. ImageView is a subView of ScrollerView. My code snippet is shown below:
imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image
//imageView.tag = i; // tag our images for later use when we place them in serial form
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
[self addSubview:imageView];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button
btn.frame = CGRectMake(215, 550, 100, 100);
[btn addTarget:self action:#selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
[self addSubview:btn]; //Buttons are clickable but shows button on all images**
//[imageView addSubview:btn]; //Buttons are not clickable and are shown on all images
I have two choices rite now. Whether i make my button a subview of ImageView or ScrollView which is parent of ImageView. If i make button subview of Scrollview like [self addSubView:btn]; it displays at right position and is clickable but problem is that it is created on all images in the queue because i am making it a subview of parent view i.e scrollview. Else if i make it subview of child view i.e ImageView which is unique for every image, but still its shown on all images and its not clickable :/
Can any1 guide me on how to make it clickable and keeping the linkage between dynamic button and the image unique so that i have different buttons at various positions on each image in the queue.
Thanks in advance.
Regards,
wahib
You have to create an extra UIView for containing both UIImageView and UIButton
UIView* imageContainer = [[UIView alloc] initWithFrame:CGRectMake(0,0,1000,1000)] autorelease];
UIImageView* imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
[imageContainer addSubview:imageView];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button
btn.frame = CGRectMake(215, 550, 100, 100);
[btn addTarget:self action:#selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
[imageContainer addSubview:btn];
[self addSubview:imageContainer];
Caution with memory leak. You have lot of unused retain in your code.