NSFetchedResultsController and my TableView structure - iphone

I have a tableView controller with two sections. The first section has a couple of input fields and is not really displaying core data. The second section displays items from a database saved with Core Data.
I have an NSFetchedResultsController and I serve up data for cellForRowAtIndexPath and didSelectRowAtIndexPath as follows. For section = 0, I manually serve up the appropriate input fields, and for section = 1 I want to use [fetchedResultsController objectAtIndexPath:indexPath]. () However, since the fetched results controller only knows about one section, this doesn't work.
I know I can create a new IndexPath with section = 0, and then feed that to the NSFetchedResultsController. Is that the preferred solution or is there another way to 'tell' the NSFetchedResultsController what to expect?

The only way to do this is to translate the NSIndexPath objects passed into the various UITableViewDataSource/UITableViewDelegate methods into index paths appropriate for your NSFetchedResultsController. I'd recommend adding a method to your class that does this.
The return values from this method will match the section numbers the NSFetchedResultsController uses. Also, if in the future you end up needing a second header section for whatever reason, it's easy enough to adjust your method to do that.

Why would you need to tell it anything? In the delegate methods, just offset the section index by one and you should be fine.
You can create your own NSIndexPath instance to pass into the NSFetchedResultsController to resolve this issue.
Update
If you want to have two sections then yes that is the right answer. However I would consider putting your input fields into the table header instead of a section. That would be a cleaner answer in my opinion.

Related

iphone: In group section tableview how do you disable section in search result?

I am using searchDisplayController and it would be searching for the names and also the sections. i would like to know how to show the results of only names and not including the section. Assume the codes are the similar as from apples doc. I have at least 2000 names in there when viewing the tableview itself.
See my answer to this question:
UISearchBar Search table row with text, subtext and image
What you show in a results table is completely up to you. It needn't even have anything to do with the table you're searching! (But of course it usually does, as otherwise you'd confuse the user.) You simply form the data that populates the results table; what data that is, is your call.
So, if you don't want to include any section titles, then when the table inquiring of your data source / delegate is the results table, don't include any section titles! It's your code, it's your table, do whatever you want. You are the one implementing tableView:titleForHeaderInSection: to return titles; if you don't want titles, return nil instead. Of course, if the data source for the real table is the same object as the data source for the results table, then tableView:titleForHeaderInSection: will have to examine the incoming tableView parameter to see whether it is the real table or the results table, and make its choice of what to return based on that.
I would like to a bit more of your problem as it is not much clear. I don't actually get this line:
i would like to know how to show the results of only names and not including the section
For searching, it is best to search in a dictionary/array and show the result in tableview by [tableView reloadData].

what is NSIndexPath

What exactly is the job of NSIndexPath,
What I understand is, IndexPath variable is used to refer the cell which we want to display ?:o ?
But what value does it store ? I mean.,
What is the internal process that happen to setup an indexPath variable
It is used as a vector for referencing arrays within arrays. For example you can represent a path to array[a][b][c] using an IndexPath. Internally it is used for iPhone list views, for example a list view that allows you to select a country, then gives you an option of regions, followed by a list of cities in the said region. The indexpath to the city you selected would include the path to it through the country and region.
Specifically for UITableViews, NSIndexPath plays a slightly more expanded role. UIKit adds the row and section properties to an instance of NSIndexPath, and a class method + indexPathForRow:inSection:.
Therefore if you want to select or delete a specific row in a table, you would need to pass an instance of NSIndexPath to that table. To create that, you would use indexPathForRow:inSection to create an NSIndexPath instance.
If you have an existing NSIndexPath that you want to get info about and it's from a table view, then use its row and section properties to get the data.
If you encounter index paths outside of the table view, be careful regarding the specifics of its use. Either way, in general, it is a reference to a specific element in arrays of arrays.
In iOS the NSIndexPath objects are always of length 2. They're used for example to index a table view cell. The first index in a NSIndexPath object is called the section, the second is the row. An index path object with section 0 and row 0 indicates the first row in the first section.
Thats all you need to know when dealing with index paths on iOS.
Apple's class reference documentation is useful only for people who HAVE the mental model instantiated into their cognitive processes. When someone asks a question such as the original post, even if they are unable to fully articulate their confusion, the class reference documentation is some of the most wonderful non-information ever published. I too am struggling to figure out how---in the words of another SO answer---to fill in "// compute some index path" (knowing only an integer offset of the cell... I want to select my nth item), and am looking for the same level of conceptual overview as the original post, so I thought I'd explain the dilemma a bit.
I'm not 100% sure what you're asking, but there is more information here.

Mapping a dataset to textfield inside UItableView

In my app, I am using some .net asmx services (as backend) to get some data and populating it on my views. In one of the modules, i need to edit and save the data which am getting from the services.In that am getting several rowkeyvalues and accordingly am creating those many row cells in my tableview, which consists two UItextfields as well, which displays some unique code and comments.
Now if I have to edit and save some fields, I need to map each one of those rowkeys to each row cell and after that am creating object which is basically the wsdl proxy class which I have generated using Sudzc and pass each dataset and serialize it to XML and POST it through SOAP.
The real problem here, am not getting how to map each unique rowkeys to each row cell and create unique dataset(which mainly contains other fields as well including rowkey) for each row and pass it to proxy object (WSDL stubs).
Not sure I understand your problem, but there are 2 ways I can think of to map a row cell to something.
1. Each UITableViewCell is a subclass of UIView, and so it has a "tag" attribute, which is an int.
You can set this and check it's value.
2. When a user taps a row, the tableView:didSelectRowAtIndexPath: is called. IndexPath.row is an int that gives the absolute row postiion within it's section (and if only one section, then in the whole table).
Usually, in cellForRowAtIndexPath: you fetch your backing data corresponding to the IndexPath.row and populate the cell. You could also set the cell's tag at that point.
This ties together the row, the cell and the data.
Hope that helps.
-Mike

Dynamic Sections UITableView with Unknown cells

Hey,
I'm basically trying to retrieve data from SQlite db and populate a tableView from it.
The sql-data-retrieval method creates two arrays. "dataArray" and "SectionArray".
DataArray is filled with data NSObjects.
Uptil this point, i can create the appropriate Section headers.
Here is the problem,
-1 What do i do to make sure that the right objects get into their appropriate sections and not under any other sections (which they seem to be doing). Also the Count(number of rows) in each section differs.
What should the code be in "NumberOfRowsAtIndexPath" and cellForRowAtIndexPath methods
-2 What kind of datasource objects are more suited for this type. I'm simply filling up two NSMutableArrays - dataArray(rows) and SectionArray(Section headers).
I think you should make many NSArray one for each table header you have created. In NumberOfRowsAtIndexPath you will return the count of the array for the requested section, and in cellForRowAtIndexPath you will choose your array using the section index (as before) and with the row index you will select the row of that array.

UITableView's reloadData isn't resorting the cells

I have a UITableView that implements NSFetchedResultsControllerDelegate. When I tap a cell, I load another viewcontroller which allows me to edit an entity represented by the tablecells. When I'm done editing, it sends a message to the UITableViewController which does a [self.tableview reloadData];
My problem is that reloadData doesn't seem to resort the data after I've modified it. How do I do this. How do I get it to do so?
Not clear what is that you are trying to do. However if you want cells to be in a different order for a table view you must sort its data source, so if first you want it in order 1 2 3, then in your data source they will be in that order, but next if you want them 2 1 3 then you must resort your data source to contain 2 1 3 in that order, it all depends on what you are returning in cellForRowAtIndexPath for each index path, the sample i gave is assuming y ou are using the row indexpath to index into an array that i s your data source..hope this helps
You can pass the NSSortDescriptor to the NSFetchedResultsController, but have you actually executed a fetch?
You need to save the changes using [NSManagedObjectContext -save:] and then your NSFetchedResultsController will pick up the change and notify the table view to update. You do not need to call -reloadData because it will be handled for you.
Make sure that your UITableViewController is the delegate for the NSFetchedResultsController so that it can handle the update.