How to call method from UItableview delegate - iphone

1) My problem: how to call NumberofRowsInSection of UItableViewDataSource Method of another UItableViewdataSource.
2 ) HOw to make aggrateTableViewDataSource from n number of Different CustomTableViewDataSource?
Need Help.
Thanks

I think there is some conceptional confusion in your question.
datasource is a delegate protocol. That means that you can have a class that adopts that protocol. That again means that it has certain required or optional methods, e.g. tableView:numberOfRowsInSection: in the case of the protocol UITableViewDataSource.
Therefore there is no such thing as "two datasources". If your class that implements the datasource protocol has more than one source for its data, that's a different kind of "data source" - and a question completely unrelated to the datasource protocol for table views.
Of course, you can have more than one UITableView that refers to the same class as its datasource. This is actually common for search tables which typically can display both the original and the search result table. In this case, you check in your datasource methods which table view is requesting the data.

I would suggest to make a model that calculates this from the source, not the tables. If you are using core data, create a class that fetches the objects and calculates the figures then observes changes to update the basis for the figures. Make your aggregate functions output as read only properties.
If not core data use, I'd need more info to advise.

Related

Referencing nsmutablearray to Data Model

I have a design issue which I am trying to analyze in my current project and thought maybe someone could help me figure this out. I have an nsarray object which I filter through predicate, and I want to set that object as my data model through view controller. First, is this a good practice of doing so? Since, I want to have an access of that object through out my transaction. I am not dealing with any database, plist, or core data model at current, these are just custom data model class I have created.
Thanks.
It's very common for a view of some sort to be backed by an NSArray, or an NSMutableArray. (Particularly, a UITableView, which can provide a single cell for each object in your array.)
Depending on the scope of your project, you can either investigate using Core Data for binding your model to your data:
CocoaDevCentral Core Data Overview
Or, for something a bit easier but less robust, you can look into implementing the methods defined in the UITableViewDelegate and UITableViewDataSource protocols, if you want to populate the cells of your table on a per-request basis.

Should I fetch all objects initially or when each view controller is loaded?

thanks right away. This is my first question and am excited to join the iOS developer community. I have one core data entity (say, a car). I have a tab view controller with two tabs - one displaying all cars and another displaying all types of cars (Chevy, Ford, etc.). The question deals with this second view controller. My question is - do I want to fetch all of my cars when the tab is loaded then pass all relevant cars of that type when the row is selected, or do I want to fetch my results after a row is selected meaning I'd have a different view controller for each type of car?
UPDATE:
I do, indeed, have two table views. The second one with the types has a list of types. When a row is selected I'm curious if I should pass the relevant cars to this VC or fetch the results?
I'd say you use 2 ViewControllers: 1 for all the Cars and then 1 for all the relevant cars of that type. You can just pass the relevant data (let's say an array of carmodels) from the FirstVC (where you put your initial array of cars , or dictionary if you will) to the secondVC and adjust your VC-looks accordingly.
(Seems you want to work with a UITableView to do this, it is very simple to pass these kind of messages through to a new VC and do the customization you want anyway)
You will all find your UITableView answers right here in the AppleDoc: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableView_Class/Reference/Reference.html
A UITableView object must have an object that acts as a data source and an object that acts as a delegate; typically these objects are either the application delegate or, more frequently, a custom UITableViewController object. The data source must adopt the UITableViewDataSource protocol and the delegate must adopt the UITableViewDelegate protocol. The data source provides information that UITableView needs to construct tables and manages the data model when rows of a table are inserted, deleted, or reordered. The delegate provides the cells used by tables and performs other tasks, such as managing accessory views and selections.
One of these delegate methods you seek is the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This method will trigger when you select a row in your tableView. In this method you want to call and show your new VC and this is also when you pass the relevant data to that new VC. The rest of the above will still hold up then.
Good luck.

What is the best practice to reference model objects in views with Core Data?

I have an NSManagedObject that described an animal, it has the attributes "name", "species", "scientificName" etc. I display all animals in a UITableView and managed the NSManagedObjects with an NSFetchedResultsController.
To provide my table view cells with the information necessary to render the content so far I just weakly referenced them to my NSManagedObject describing the animal. However, sometimes the NSManagedObject should get faulted and when the table view cells are redrawn, they re access the properties of the object and thus prevent the fault.
What are the best practices to provide view objects with information when using core data as data source? If possible I would like to avoid copying all attributes of the NSManagedObject over to the table view cell.
I believe it is a good practice to clearly separate the Model, View and Controller layers. Especially making sure the Views are not holding Model state. Holding on to a NSManagedObject holding on to a Model object. Copying some data is unavoidable.
I usually implement a method for "painting" the View with the Model data. Something like this in the UITableViewCell subclass:
-(void)configureWithAnimal:(NSManagedObject*)animal {
self.nameLabel.text = [animal valueForKey:#"name"];
self.speciesLabel.text = [animal valueForKey:#"species"];
// Etc.
}
This way it is a single line of code in the UITableViewController subclass to setup the cell, independently of newly created or reused cells. And if many tables wants to reuse the custom cell then not all f them need to reimplement the code to seeing up the cell.

ASP.NET MVC 2: Use [Data Annotations] to reference methods that could feed DDL lists?

A generally accepted way to pass all data to a view is to have a single data model with references to both your domain model and additional data for things like drop down lists (DDL).
However, partial views (view templates too) receive only a portion of the main model, not able to access the root of the Model sent to the original view. So if your DDL lists are not static, how do the partial views get the data?
Is there a way using [Data Annotations] to reference a method which could return the possible values of a field, then use this in the partial view's DDL? Where would this method exist, in the repository?
Links or C# code examples would be very helpful.
There is no built in Data Annotations attribute that could do what you ask.
You could create your own attribute that contains a reference to a Type and the name of a static method that you can then invoke via reflection from your partial view.
Where you would place such a method depends on what you are doing, though I still think that gathering all the inputs in your controller would be better. You can always set extra items in the ViewData collection and pass those into your partial views.

One UITableView - Multiple DataSource, best design pattern?

This seems like a typical problem, but I have a UITableView that has identical behavior for two separate data sources. What is the best way of going about designing the class hierarchy to have as little duplication and if/else conditions? The view controller is going to do the same exact thing to both data sources, they're just unique in their data set. Should I have the parent controller just set its data source/respective title?
The same issue is relevant as well when using a UISegmentControl for displaying two views with the same interfaces, but different data sources.
Be careful with your terminology here. A UITableView has something called a dataSource but you seem to be referring, essentially, to two different sets of data.
In the case you're suggesting, in the table's dataSource (the object that adheres to the UITableViewDataSource protocol), I'd have three arrays.
currentlyViewedArray
datasetOneArray
datasetTwoArray
In the dataSource methods, use the currentlyViewedArray as the source of the table's data.
Then, set the currentlyViewedArray to whichever array you want to view:
self.currentlyViewedArray = self.datasetOneArray;
[theTableView reloadData];
You can use the UISegmentedControl to switch between the two arrays.