Say i have a list of food table and display all food categories such as american, ltaliano and japanese. then when i click american it will show list of american food such as hamburgers. please help.
There two ways to store data as
1) In instance variable, NSDictionary
NSDictionary *dict = #{#"american":#[#"hamburgers",#"french fries"], #"ltaliano" : #[#"Insalata caprese",#"Pasta e fagioli"], #"japanese": #[#"Udon noodles",#"Donburi rice bowl"]};
Load your table using NSDictionary's [dict allKeys] method and when you click on a row, get keyName from NSDictionary's and pass values for this key into the next UIViewController
2) Store your data in the SQLite local database with FoodCategory(have all food categories) and FoodDetails(food category and its foodName) Table. In first UIViewController, to display lists of food, get foodCategories from the FoodCategory table and store it into NSArray. Using this NSArray load your table. When you click on any row pass selected foodCategory to the next UIViewController and in this to get its foodNames, write SELECT query to fetch all records from FoodDetails table for selected foodCategory.
For tutorial:
Basic UITableView tutorial1
Basic UITableView tutorial2
Hope this will help you.
Related
I have a coredata app with an Entity called Product.
In my interface I have an array controller for this entity called ProductAC. I have bound a table view and text fields to the array controller and can see the details, and I can add objects to it.
Now I want to create an export file for some of the objects. In the tableview I can select them, and then in the code I have this:
NSArray *uploadProducts = [ProductAC selectedObjects];
NSEnumerator *loop = [uploadProducts objectEnumerator];
This produces a nil value for the uploadProducts array.
How do I select the items to further process them? I have cleaned the file, closed and re-opened Xcode but I cannot seem to 'grab' the selected objects from the tableview?
thanks
Make sure you have the table view selectionIndexes property bound to the corresponding property on your array controller.
Thanks for reading:
I've gotten stuck on this problem before and I know there is a solution, but after a a few hours of searching I haven't been able to find it again.
I'm using an NSForm and an "Add" button to create a new instance of a student and add that student to an array (sessionRoster). No problems here.
- (IBAction)addNewStudentButtonPressed:(id)sender {
//CREATE NEW STUDENT
Student *newStudent = [[Student alloc] init];
//ASSIGN VALUES FROM FORM
newStudent.firstName = [[studentForm cellAtIndex:0] stringValue];
newStudent.lastName = [[studentForm cellAtIndex:1] stringValue];
//ADD STUDENT TO ROSTER
[sessionRoster addObject:newStudent];
//CLEAR FORM
[[studentForm cellAtIndex:0] setStringValue:#""];
[[studentForm cellAtIndex:1] setStringValue:#""];
}
I'm using an Array controller to display the array in a tableview. No problems here. I have the bindings correct, because I've included some "dummy" students in the init, and they appear when the program is run (I wanted to post the screenshot, but I don't have enough reputation).
My question is, how can I make the table update each time the "add" button is pressed?
I do have the table as a property, and calling [tableView reloadData] doesn't work. I believe the solution before was some kind of "contentWillUpdate:YES" and "contentDidUpdate:YES" pairing, but I can't find that information again.
Thanks for your time!
J
Add the student object to the arrayController not to the array. The array controller "does the lifting."
How can I have more than 2 values held by my table view cell? What I want to happen is when I click a cell in my table view, it will pass 3 values to the next view. But I can only get two values (name, address) because they are contained in the textlabel and detailtextlabel but the (phone) is not being showed in my cells, only name and address.
If u want to display more value in cell the create custom cell.
Option is use NSMutableArray with like below and show all values in custom cell:
NSMutableArray *arrValues = [[NSMutableArray alloc] initWithObjects:[NSArray arrayWithObjects:"Value1","Value2","Value3"],nil];
Add as many array into arrValues.
Now when tableViewDidSelectAtRow 's method: pass this array to next controller
NSArray *arrPassing = [arrValues objectAtIndex:indexPath.row];
create a custom cell for the tableview cell
I am making an app where I'll have a tableview with an 'add' button in the navigation bar, and this 'add' button creates a new row, but the problem is I can't figure out how to give rows unique names. To let the user decide the name, I have included an alert with a plain text field. The user should then write the name in the field and the new row will then be called that. But how do I do that?
I have used the tableview template provided in Xcode, which give each row a new name according to the clock they added a new row. I'll just want a alert to pop up first and then let the user type the name for the new row.
You have to identify each row somehow. One way is to use the index in the table as the identifier. I guess your adding the row to a NSArray which is mapped to the table rows or something similar, so you know the index of the new row. You only have to update the row of that index using this method on your UITableView:
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
Assuming you have only one section, your array is called 'arrayWithRowsData' and you put the new row at the end, you can create the index path with:
int newRowIndex = [arrayWithRowsData count] - 1;
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
Hope it helps.
Sorting Bound Data in NSTableColumn using IB binding.
Keys: NSTableColumn , sorting, NSArrayController, Content Set
A contentSet serves like a data source for a TableColumn
This deals with a SplitView with two single column NSTableViews
The names of the TableViews are BookCategory and
Books.
The Books Table has a single column with book_titles.
The Class BookCategory has a one to many relationship
to Book.
The BookCategory Table is sorted at load using:
#implementation MyBookCategoryController
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
NSSortDescriptor *descript =
[NSSortDescriptor sortDescriptorWithKey:#"name"
ascending:YES selector:#selector(caseInsensitiveCompare:)];
[self setSortDescriptors:[[NSArray arrayWithObject:descript] autorelease] ];
}
return self;
}
This same approach fails to sort the BookTitle table at load. !!
The BookTitle table/column loads unsorted.
For the TableColumn the Attributes Inspector has
Sort Key:title
Selector: caseInsensitiveCompare:
Order: Ascending
This appears to enable the sorting when one clicks
on the column header.
I want the data sorted when the view loads.
The binding inspector for this book_title column has:
Value : BookCategoryArrayController.arrangedObjects.name
The BookTitleArrayController in binding inspector shows
Content Set: Book Category ArrayController.selection.books
To restate the problem, the tableview with the book titles
load unsorted. It sorts only after the user's FIRST click on the
column header.
Say there are three book categories Art, History, Sports.
When the app loads the left table in the splitview is sorted,
that is :
Art
History
Sports
When the user selects ANY category, titles for all books
in the category appear in the right tableView but unsorted.
If the user clicks on the Header of the book_title TableColumn
the initial sorting is done on the column. Thereafter
the selection of ANY book category causes a sorted display
of book_titles in the right tableView. That is, ONLY the first
selection of a category results in an Unsorted book title list.
Thanks a lot for reading, Mark
This is an outline of what finally worked for me.
Sorting with CategoryArrayController (NSArrayController)
Bindings Inspector
Content Set Bind to: CategoryArrayController.selection.books
SortDescriptors Bind to: ShareUserDefaultsController
Controller Key: values
sortDescriptors (note: exclamation point)
NSUnarchiveFromData
Shared User Defaults Controller
Referencing Outlets
userDefaultsController -- AppController (C:AppController)
Referencing Bindings
values.sortDescriptors -- Sort Descriptors Rx Array Controller (C:NSArrayController)
-- Sort Descriptors Category Array Controller (C:NSArrayController)
Clicking on Category Header does no sort. Selects all in Cats, and empties Recipe Table
Comments on the above welcome.
TableViewCat (NSTableController)
Attributes Inspector
TableView
Selection
Empty,Column,Type Select
Control
State.Enabled
Connections Inspec
None
Bindings Inspec
None
TableColumn -Category (NSTableColumn)
Attributes Inspec
Sort Key : None
Bindings Inspec
Value -- Category Array Controller.arrangedObjects.name
Sorting with RxArrayController (C:NSArrayController)
Attributes Inspec
Array Controller: 1-5 of 6
Object Controller
EntityName: Recipe
Prepares Content
Editable
Connections Inspector
Referencing Bindings
arrangedObjects.name -- Value - Table Column Recipe
selectedObjects -- Double Click Argument - Table View Book
Bindings Inspector
Controller Content
Content Set
Category Array Controller.selection.books
Controller Content Parameters
Sort Descriptors Bind to: ShareUserDefaultsController
Controller Key: values
sortDescriptors (note: exclamation point)
NSUnarchiveFromData
Parameters
Managed Object Context(App Delegate.manangedObjectContext
TableView Book (NSTableView)
Attributes Inspector
TableView
Selection Empty,Column,Type Select
Control
State.Enabled
Connections Inspec
Bindings
DoubleClick Argument -- Book Array Controller.selectedObjects
Double Click Target -- App Delegate.self
Bindings Inspec
Double Click Argument (Book Array Controller.selectedObjects)
Double Click Target (App Delegate.self)
Bind To: App Delegate
Model Key Path: self (Repeats ?)
Selector Name: newBookAction:
Conditionally Sets Enabled
TableColumn - Book (NSTableColumn)
Attributes Inspec
Sort Key : None
Connections Inspec
Bindings
Value -- Book Array Controller.arrangedObjects.name
Bindings Inspec
Value -- Book Array Controller.arrangedObjects.name