UITableView Section Indexing - iphone

I'm creating a tableview with an index, my index array has 26 values (A-Z) but my data source may only have a few sections (A,D,S).
My challenge is that the user selects an index value but the incorrect section should not display, if I'm not mistaken this is due to the index value returned is different to the selected value for row.
In my example above selecting index 'D' will return a value of 3, but the section value for 'D' in my data source is 1. Is this normal, if it is hows the best way to handle this?
Thanks

I believe that this is the correct behavior. Solution would be to create your index with only the values that actually contain data.
When more data is added, add new sections if required.

Related

Saving Order on UITableView data

I have created a reorder-able UITableView. I am loading data from Realm, and showing that data into UITableView. My UITableView has Section and rows.
now User can hold Any cell in any section and can reorder them in 2 ways
User can reorder cell with in the same section.
User can reorder cell across the different section.
What I need:
Once the user has made the order he should see same item in same order
when he come back to App. For any type of reordering I have to do
indexing. But I do not have clue how to reorder them in database.
My Idea to resolve the requirement
My idea is to include the String type value in the Model where I can replace something like (1,10) that will indicate section 1, row 10.
But I do not have idea how to tackle this and how to save the reordering as i need here. Any idea about that?? please share

Suggestion about keeping a track of fields

I am posting this message to get a suggestion about the best approach.
I am making a program in which users select the fields that they want to use. The list appears in the form of a table on the next screen. Depending on the chosen fields, the placeHolder in the textFields change. Therefore, I have to keep a track of selected fields.
I have considered the following approaches so far:
Two arrays. One stores the list of the labels on the cell and the second array stores 1/0 values. 1: field is selected, 0: not selected. Therefore I will have to obtain the name of the field from array 1 and whether it is selected or not from the second one.
One array containing the 1/0 indicating whether the field is selected or not. I traverse the array to determine whether which placeHolders should be shown.
NSDictionary: It will contain object:"name of the fields" and key:1 or 0.
I don't find either of these options as good programming practice because there are lot of if and else statements and plus, there is a tremendous amount of repetition in the code.
Can you please suggest the best way to approach this problem?
Thanks!
Not sure why you think there are a lot of if/else statements. You can do it with an array of dictionaries, where each dictionary contains two keys: field name and selected. Fieldname is NSString, selected is NSNumber wrapping a boolean value. Then in your cellForRowAtIndexPath you have something like:
NSDictionary *cellData = dataArray[indexPath.row];
cell.textLabel.text = cellData[#"field_name"];
if ([[cellData#["selected"] boolValue] == YES) { ... do whatever you do when the cell is selected, like add a checkmark ... }
You might also think about making a custom data class to hold each cell's data. You can create properties for the label name and the selected-ness (this will avoid having to wrap and unwrap NSNumbers to represent YES/NO). You could define methods on the custom class that return the correct label depending on the selected-ness. Then you just create an array of these custom objects.
I guess what you want is to figure out which one has been selected. You can use a number to mark up which one is selected.
Let's say there is 3 fields: field0 field1 field2 and a number in binary 000. Once field1 is selected, then mark the number as 010. Pass this number to the next screen, then you can use & for every filed.
For field0, you may do 010 & 000 and get a result 0 which means field0 is not selected.
For field1, you may do 010 & 010 and get a result no 0 which manes field1 is selected.
For field2, you may do 010 & 100 and get a result 0 which manes field2 is not selected.

In What Order UITableView sections are displayed?

I am getting data from a sqlite3 database and i'm saving those things in an array and i'm using that array in viewForHeaderInSection method, every time my data will be different i mean section list will be different every time it is displaying randomly i need to display it in an order.
1. How a UITableView displays its sections(any sorting algorithm is there) or randomly it will display?
2. How can i sort my sections in an order?
Any help is thankful in advance.
The table view displays its sections in the order you tell it to. If you are populating an array for the sections from a SQL query, you can apply ordering in the query. If this is not possible, you can sort the array after population using one of the many sorting methods available on NSArray, for example sortedArrayUsingDescriptors:. See here for more:
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/sortedArrayUsingDescriptors:
Question is a bit unclear. Better if you can post some code. You get different data from sqlite each time into array and that data is shown in sections. So why do you need to sort sections ? Instead, sort the array according to requirement and show the sorted data in section.
Are you asking about the order of the sections or the order of the content within the sections? For example:
Section 1
Section 3
Section 2
OR
Section 1
Row 1
Row 3
Row 2
Only the order of the Rows is determined by the order of the datasource of the uitableview.
i think you are getting different unordered data form sqlite database into array.you want sort the array(arrList)data
arrList = [arrList sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
NSLog(#"%#",arrList);

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.

iPhone UITableView populating variable rows sections from flat array

I thought that would be very common and easy iPhone App. In the main app I connect to database, retrieve values from database (NSDate converted to NSString)n and put into single array. Then in one of the views I populate UITableView with elements from the array. UITableView is grouped (sections). I step through array to discover number of sections (change section if new day). How do I retrieve correct element of array in cellForRowAtIndexPath? IndexPath.section and IndexPath.row seem useless as row starts count from zero for each section. If number of rows in each section was the same it would have been easy:
[arryData objectAtIndex:(indexPath.row)+indexPath.section*[tblMatchesView numberOfRowsInSection:indexPath.section]];
But number of rows in each section varies... :-)
Separate your data into arrays of arrays (based on the number of different days) once you get it from the database, that'd be the simplest solution...
How about storing different date sections in different arrays? For example, you can have an array A of array. You can loop through the original arrays yourself, if you found a new day, you just create a new array and put it into the array A. And then, when you loop over the cell, you can get the section number to get the correct array and based on the row number to get the correct elemenet in the array
It doesn't have absolute cursor but you can try utilizing
UILocalizedIndexedCollation class which is used to ease the "sectioning" of your data and proving the tableView delegate functions the required data such as the index titles, etc
Here's apple documentation link for it:
https://developer.apple.com/library/ios/#documentation/iPhone/Reference/UILocalizedIndexedCollation_Class/UILocalizedIndexedCollation.html#//apple_ref/occ/cl/UILocalizedIndexedCollation