How to reuse UITableView from another class - ios

in my apps i want to to show a UITableView that exactly same with another UITableView from another class.
Let say in my HomeClass.h i already have UITableView property:
#property (nonatomic, strong) IBOutlet UITableView *fromTable;
and already success implement this fromTable.
However, i also want to use this fromTable again from other class, lets say DetailClass.
So when for example, i push a button in DetailClass, the button will show fromTable, also with all the delegates function like didSelect, etc.
Can i do that instead create other UITableView again in my DetailClass?
Thanks...

Refactor the UITableView's Delegate and DataSource methods into an object that you use in both Classes (presumably UIViewController subclasses).

Related

How to access programmatically a tableview added with storyboard

I added a tableview to my viewcontroller using the storyboard. What is actually happening behind the scenes? If a UITableview object gets created how can I access this object programmatically? What is the name of the instance? Is it a property of my viewcontroller object?
More specifically, what I need to do is force my tableview to refresh (from inside of my viewcontroller). I read in other posts that I should be able to do something like this [self.tableview reloadData]. I can't do this because my viewcontroller does not have a property called "tableview" (or anything similar)
Control + click and drag your tableView into your header file to create an IBOutlet
Set the delegate and data source by control click and drag from your table view to your view controller - make sure both dots are seen. Now
In the interface .h file add the delegate and datasource as such:
#interface HomeTableViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
Now your view controller should have access to the methods needed to use [self.tableView reloadData]
try this simple table view
and this work with custom tableview cell
try above tutorials and in your case to use [self.yourtablviewname reload]
you have to create an outlet of your table view and use it.
Remember that the storyboard defines a collection of objects which are created when you make an instance of say, a ViewController.
You are on the right track, you need to define variables in your view controller which are references to the objects in the storyboard. You mark them as IBOutlet in your code. You then 'connect' them to the objects in the storyboard. Right click on the viewcontroller in the storyboard and you will see the variables which have been designated as IBOutlet listed as 'outlets'. You can drag from the popup table to the objects in the storyboard to connect them. Now, when an ViewController is created, all the IBOutlet variables are set to point to the various objects (tables, text views etc) in the instance just created.
Remember that the 'connect' you do with the Interface Builder, happens at rum time when you create an instance of the object in IB.

providing common functionality for different viewcontrollers in objective c

I have many viewcontrollers which needs to have some common functionality related to navigation.
Earlier I made a base class BaseViewController(extending UIViewController) which have all common functionality (like doing some tasks on viewDidLoad etc) and all my viewcontrollers extends BaseViewController.
The problem is that some of my viewcontroller should be subclass of UIViewController and some of UITableViewController, so I can not use above approach.
One way could be to write base class for both and duplicating code. Is there any better way without duplicating code.
While you can get around this by using delegation or helper objects, I would make the case for just not using UITableViewController. It is only a very light subclass on top of UIViewController, providing a table view, conforming to the delegate & data source protocols, and adding a property or two for selection & refresh.
While I wouldn't normally suggest recreating something that the framework has already done for you, it may (in your case) make your code more easy to understand if you just keep everything inheriting from a common base class and add a table view to one of the subclasses.
If you do think this would be a reasonable approach, the UITableViewController documentation overview gives a detailed description of exactly what & where these behaviours are implemented, so mimicking its exact setup is trivial.
Adding a table view to UIViewController
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) IBOutlet UITableView *tableView;
#end
In your storyboard, drag a "Table View" from the object library and drop it on top of your View Controller scene's "View" in the Document Outline - this will replace the root view with a UITableView.
Then just hook it up:
ctrl-drag from the view controller to the table view to hook up the view and tableView outlets
ctrl-drag from the table view to the view controller to set the delegate and dataSource outlets.
Done - no magic required.

Display object on all view controllers

I have several objects which I display throughout all of my ViewControllers.
I am wondering if there is a way to merge these objects into a single object and initialize the object from every ViewController in the project.
In essence, I am attempting to merge the objects (they are the same type and do the same thing) and make them globally accessible.
Ex.
#property (weak, nonatomic) IBOutlet UIProgressView *progressView;
I would like to display and access this UIProgressView from all ViewControllers.
You can put these interface elements in a UIViewController subclass and then have all of your other viewControllers inherit from that one (i.e. an abstract superclass) in this way there is only one lot of code which covers them all.

Can a property of a UITableView be the UITableView Delegate and DataSource

I have a custom view that's a bit of a hack. Basically it's a UIView with tableView as it's property, with additional views in the tableView that need their own delegates. I need a viewController for the UITableView and can't make the UIView it's delegate according to this SO link Custom UIView as UITableView delegate and datasource?.
Can I make the UITableView have a property of UIViewController and set that UIViewController as the tableView's delegate?
In this case according to OOP, the UITableView has a UIViewController so technically, I could expect this to work. But, I am wondering if down the line somewhere this could create problems since the UITableView and UIViewController are coupled in this way.
You don't need a UIViewController for the UITableView - you just need an object or objects that implement the data source and a delegate protocols. As per the accepted answer on the question you linked to you can use a separate controller class to provide this.
The right answer depends a little on how the table is used with your UIView subclass.
If the table will always have the same content (Say a list of months) and there is no value in exposing or abstracting the properties then you can code the delegate and dataSource inside your UIView subclass or in an assistant class.
If the table content will vary depending on how the UIView is used (say a list of people where you don't know what the list is - friends, relatives, employees...) then it would make sense to simply expose the tableview's datasource (and delegate if necessary) properties via your UIView subclass

Reference position in CollectionView IOS

Problem with reference position in CollectionView
I made a simplified example of my problem. I'm using storyboard with a CollectionView, but then I leave that view and return the elements out of place.
The problem occurs after I connect the
# property (nonatomic, strong) IBOutlet UICollectionView * CollectionView;
Anyone been through this?
https://www.dropbox.com/sh/rmkkcp99al6czy6/-nL6fUYRQS!
image with the problem
First, you should not have your property names start with a capital letter. They are not classes. Better use collectionView.
Second, make sure your collection view is wired up with the appropriate controller in storyboard. You need to connect it to the delegate and datasource properties. Make sure the view controller implements these protocols.

Resources