Reusing a UITableView footer view in multiple views - ios

I have a footer for a UITableView which is a "complex" view (textView and a button).
I made a xib and added it to the footer of the UITableView.
The next stage is having referencing outlets in the ViewController of the UITableView
The thing is that I want to use this footer view in several ViewControllers (and not DRYing)
What can I do?

It sounds like you want to have a xib/.h/.m separate from the .xib used by your UIViewController. This is for your View that will be used as the footer of the UITableView. You can [[MyFooterView alloc] init] in the viewDidLoad of any controllers that are using a UITableView and assign the footer to that instance of MyFooterView. Is this what you're after? You can still have referencing outlets to the UITableView in any UIViewController, and keep a reference to MyFooterView that you instantiated in viewDidLoad
addTarget example:
myFooterView.button addTarget:self action:myCustomHandlerFunction forControlEvents: UIControlEventTouchUpInside

Related

Add view behind tableview in UITableViewController

I'm trying to add this custom control below my tableview in a TableViewController:
https://github.com/zogieosagie/RMEIdeasPullToSortControl
In the example the creator gives, the control is implemented using a ViewController and an added tableview, but I want to use it in a TableViewController. I have created and initialized it as shown in the example but I cannot get it to show up behind the table. Any ideas?
Here is a screenshot of the control above my tableview: https://www.dropbox.com/s/ojfpacxelcy9cqm/Photo%20May%2028%2C%208%2057%2035%20PM.png
Here is my code in the viewDidLoad method:
[self.tableView setBackgroundColor:[UIColor clearColor]];
self.rmeideasPullDownControl = [[RMEIdeasPullDownControl alloc] initWithDataSource:self delegate:self clientScrollView:self.tableView];
self.sortTitlesArray = [[NSArray alloc] initWithObjects:#"Listed from A - Z", #"Listed from Z - A", #"Brand value: HIGHEST - LOWEST", #"Brand value: LOWEST - HIGHEST", #"Founded: OLDEST - NEWEST", #"Founded: NEWEST - OLDEST", nil];
CGRect originalFrame = self.rmeideasPullDownControl.frame;
self.rmeideasPullDownControl.frame = CGRectMake(0.0, 45.0, originalFrame.size.width, originalFrame.size.height);
//It is recommended that the control is placed behind the client scrollView. Remember to make its background transparent.
//[self.view insertSubview:self.rmeideasPullDownControl belowSubview:self.tableView];
[self.tableView addSubview:self.rmeideasPullDownControl];
[self.tableView sendSubviewToBack:self.rmeideasPullDownControl];
Table view controllers do not lend themselves to managing anything other than a table view. In a table view controller the content view of the view controller is the table view.
You should not try to add other views as subviews of a table view.
Those 2 things combined mean that you can't do what you are trying to do.
Instead, you should create a regular UIViewController. In your storyboard, add a container view to the view controller's content view. Create a UITableViewController as a separate scene, and then control-drag from the container view onto the table view controller. That will set up an embed segue, so your table view controller becomes a child view of the regular view controller. Now you can do whatever you want to the main view controller's content view, including adding other views behind the table view.
Do you mean that you are using a Table View Controller on the storyboard? Or do you mean that your backing code is a subclass of UITableViewController?
I haven't used this project before but I'm guessing you are using a Table View Controller on the storyboard, in which case there is no backing view for the RMEIdeasPulldownControl to attach to (the top-level view is a UITableViewController). If you look in the example it needs to be attached to a scrollview (like a table view) but it needs to be inserted into a view (like a UIView)
If you meant the second one then I'm not sure, UITableViewControllers are subclassed from UIViewControllers and are really very similar, so I can't imagine any trouble arising from that.
It isn't possible directly, but you can create UIViewControllerClass with relevant storyboard UIViewController
add a MyUIView in hierarchy then UITableView next to MyUIView
attach datasource and delegates for UITableView and use MyUIView as per your requirement.

Can't place UIImageView behind UITableView in storyboard

I can't seem to position a UIImageView behind a UITableView on my Table View Controller. I'm trying to do this within the storyboard designer. Moving a UIImageView onto the View Controller just stacks the imageview onto the tableview. In previous versions of XCode I had a window that allowed me to change the stack order of views within a view controller. Is this still possible?? I've tried dragging the views around in the jump bar and this doesn't work. So how do I accomplish this using XCode 5?
Thanks!
Use UIViewController for parent of UITableView!
Or if you want to set Background of tableView, can use this code
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"bg"]];
UITableView has a backgroundView property. You will need to set it in code.
In your example, you're probably using a UITableViewController in your storyboard. This means that the root view is a UITableView and you can't add a UIImageView as a subview to a UITableView and put it behind the superview. Doesn't make sense.
In a previous version that you've made, I suspect that you had a UIViewController with a UIView root view, which you added a UIImageView subview and a UITableView subview on top of that.
Every UIViewController is associated with a UIView, i.e. [viewController view]. In the case of UITableView, [viewController view] is of type UITableView, so when you add subviews to it, you're adding to the UITableView, not a superview of type UIView as you seem to expect.
I would recommend using a UIViewController to control that view, adding subviews of types UIImageView and UITableView.

tableView initwithframe and storyboard

I'm trying to add a tableview as subview to my tableViewController, but I want to setup the cells in storyboard. It will be a static tableview.
This is the code for calling the tableview on button click.
- (IBAction)botaoAdicionar:(id)sender {
AtividadesPraticadasTableView *tableview = [[AtividadesPraticadasTableView alloc] initWithFrame:CGRectMake(0, 170, 320, 320) style:UITableViewStylePlain];
[self.view addSubview:tableview];
}
In the tableview class I have this:
#implementation AtividadesPraticadasTableView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
Now, I have a viewcontrollerin storyboard with a tableview, which the class of the tableviewI changed to this file AtividadesPraticadasTableView. It has three static custom cells in storyboard, therefore it opens a blank default tableview.
What am I missing?
Static table views are entirely contained within the storyboard, and require information from the storyboard to display their content.
You've defined a static table view controller in the storyboard, populated it and set the tableView's custom class to your custom class, but when you want to add the table view you are just instantiating a table view of that class. That isn't going to get any of the information you've added to the storyboard.
In addition, the static cells information is read and understood by the UITableViewController, not the UITableView, so you are at the wrong level there too.
You need to do the following:
Get a reference to the storyboard, either from your original view controller's storyboard property (if it is on the same storyboard as your static table) or using storyboardWithName:bundle:.
instantiate the table view controller from that storyboard, using instantiateViewControllerWithIdentifier:. This will create a table view controller object containing all your static cells
Add this as a child view controller to your original view controller, using addChildViewController:
Add the table view controller's tableView as a subview
It may be simpler to add a container view in the storyboard to hold this view, and reveal it when the button is pressed, as Mike Slutsky suggested - this will do all of the instantiating and adding and child view controller-ing work for you, but the principle is still the same.
Also, adding a table view as a subview to a table view controller sounds very dodgy - a table view controller already has a table view as its view, and you can't really add subviews to that.
The thing your missing is the association between the programatically instantiated tableview and the UITableView that you put in your storyboard. You cannot just draw UITableViews in your storyboard and start instantiating new UITableViews in the controller's code and expect xcode to know which UITableView you wanted to use from the storyboard. Use an IBOutlet to connect a global variable for the controller to the UITableView in the storyboard, then the controller will know what you're trying to refer to. If you want that UITableView to appear on a button click, simply set the UITableView to hidden by default in the storyboard and then unhide it when the button is pressed.
The thing You are missing called manual. Check this protocol for TableView dataSource https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDataSource
P.S. Here good tutorial for storyboards http://maniacdev.com/ios-5-sdk-tutorial-and-guide/xcode-4-storyboard

UITableViewController with TableView and a non scrolling footer view

I'd like to implement something that looks like a media player, a playlist in table view that takes 3/4th of the screen height and a 1/4 height fixed panel at the bottom for play controls. I can't add anything to my UITableViewController in Storyboard, it only allows adding something to my TableView. How should this be done in iOS?
A UITableViewController inherits fromUIViewController. There is two possibilities to add others element with your tableview :
In the viewDidLoad method of your table view controller, replace it's view (it is its table view at the moment) by a new view, resize the table view, and add a view at the bottom programmatically.
Replace your UITableViewController in your storyboard by a simple UIVIewController. You add in it your tableview, your bottom view, and everithing else you want. In the header file of your view controller, specified it implements the delegates methods for a tableview :
#interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
And don't forget to assign the delegate and datasource of your tableview in the IB to this view controller.
Then write the methods required for a tableview datasource and delegate (tableView:cellForRowAtIndexPath: , numberOfSectionInTableView:,...)

View added to superview disappearing when navigating tableview

I have a tableView that navigates to a detail view when the user selects one of the tableView cells. This main TableView is created in a UITableViewController class. (MasterViewController)
I used StoryBoard to create the master-detail tableviews.
Within MasterViewController, I lay a tableView over top of the main tableView when the user selects the To button. This second tableView allows the user to select multiple values from this list.
In order to prevent this second tableView from scrolling on the screen when the first (main) tableView scrolls, I have added this second tableView to the superView of the MasterViewController view. ([self.view.superview addSubview:_toView];)
So, in MasterViewController, I add a TableViewController (_toController). I instantiate a UIView (_toView), add a background image to it (_toViewBG) and then add a TableView to it (_toTableView). I then add _toView (which contains this second tableView) to the superview of the MasterViewController view.
_toController = [[ToTableViewController alloc] init];
_toView = [[UIView alloc] initWithFrame:CGRectMake(10,10,263,247)];
_toViewBG = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,257,232)];
_toViewBG.image = [UIImage imageNamed:#"toViewBackground.png"];
[_toView addSubview:_toViewBG];
_toTableView = [[UITableView alloc] initWithFrame:CGRectMake(20,30,220,180) style:UITableViewStylePlain];
[_toTableView setDelegate:_toController];
[_toTableView setDataSource:_toController];
[_toView addSubview:_toTableView];
[self.view.superview addSubview:_toView];
[_toTableView reloadData];
This approach keeps the second tableView from scrolling when I scroll the main tableView. The second tableView stays on top and in place as the main tableView scrolls.
If I select a cell in the main TableView, I navigate to a detailed view. When I return to the main TableView, I am unable to get the second TableView (_toView) to be displayed.
I am not sure how to tell the _toView to come to the front. I realize it is added to the superview of the MasterViewController view. From some experimentation, this superview appears to be a CALayer.
Can anyone suggest how I would get this _toView to display in front of the MasterViewController view?
Thanks in advance for any assistance.
Solved
I ended up just adding the second tableview as a subview of the MasterViewController view.
[self.view addSubview:_toView];
I then disabled scrolling on the main tableview while my second tableview is visible.
Self.tableView.scrollEnabled = No;
Seems to be working fine.
Tim
It may be possible to make this work by calling [self.view.superview bringSubviewToFront:_toView], but my advice would be to make MasterViewController a subclass of UIViewController rather than UITableViewController, and then add both of your UITableView objects as subviews of the root view, (rather than referring to superview).

Resources