Suggestion about keeping a track of fields - iphone

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.

Related

Filemaker: Set field default value to 1

This might be a very stupid question but for the life of me I can't get it to work. I want to create a field in my table called "One" and it's value for all records in the table needs to be 1.
E.G.
Field1 One
A 1
B 1
C 1
.
.
.
I set the field to a number and tried the auto enter data and typed 1 in the data field but it's not updating the values.
Any help is greatly appreciated!
Thanks in advance
Simply put, Auto Enter applies to when the record is first created. One can do various other options, but this is the basic use of Auto Enter on a field. You set the value to be auto-entered in the field's options panel under Manage Database; a fixed value, calculated value and so on. This works on all versions of FileMaker.
That said, if this is to be a static value of 1 for all records, you might want to look into a calculation field with a result of 1 and possibly using global storage. This will not work if you need the user to be able to change the value.

NSFetchedResultsController predicate to eliminate duplicates of several properties

I am using an NSFetchedResultsController in my UITableViewController.
Is it possible to specify a predicate that will not retrieve items which have duplicate fields in x number of fields that I specify.
For example, I want to search all results for items but if the itemName AND itemDescription AND itemQuantity are the same, I want only one of these items.
Option 1
When the page loads do a single run through the data and keep a list of objectID that are duplicate. For duplicate object set the row height of the cell to be 0. So they are technically still there, but you can't see it. This make dealing with the NSFetchedResultsControllerDelegate calls easy because no indexPaths have changed
Option 2
If the dataset is always selected in the same way and an object that is a duplicate is always a duplicate you can set an 'isDuplicate' in the object and filter it out in the predicate. Or you can not store at all in the first place. If objects are displayed in different sets and in different way and sometime should be displayed and sometime not be displayed this is not a good solution
Option 3
If you are sorting by the same criteria that make an object duplicate (that is duplicates always appear right next to a non-duplicate) and you are NOT using sections, then you can use sectionKeyPath. SectionKeyPath groups items together into sections. Group the duplicate and non duplicate together and then display every section as a single row (use the first item in each section). The indexPaths of the fetchedResultsController will not match the indexPaths of the tableview so you have to careful to convert them.
Option 4
Instead of accessing the objects from a fetchedResultsController do a fetch and and filter the array. Then use the array to display the objects. The downside is that you don't get updates on when objects change. This can be especially problematic is objects are deleted, as accessing a managedObject that's entity was delete can lead to a crash.
I recommend option 1

Sorting UItableviewcell according to different conditions

I have customized uitableViewcell in which each cell show four data which are related to each other(ie object name,details, new price and old price). Data for each label Come from four different array.It is working fine. But now i want to sort table according to ascending price list or alphabetical order of object name on a button click. Problem is if I sort only the corresponding label change in each cell, others label remain same. i want to reorder each cell not individual label as four label are related to each other. Should I use NSDictionary for this or something else.Please guide me how to do it. Thanks.
This does not really have anything to do with the Table. You need to think data structures.
If I were you I will define a class with 4 attributes
Say
Class TableData
{
NSString * name;
NSString *details;
int newPrice;
int oldPrice;
}
Or something like that.
And now you can have ONE array with objects being instances of the above class.
Now you can sort the array by instance.name or instance.newPrice and relations will stay intact.
You then will just reload the tableView.

UITableView Section Indexing

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.

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