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.
Related
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.
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'm using an NSTreeController to manage nodes for an NSOutlineView. When the user adds a new item, I create a new object and insert it:
EntityViewEntityNode *newNode = [EntityViewEntityNode nodeWithName:#"New entity" entity:newObject];
// Insert at end of group
//
NSIndexPath *insertAt = [pathOfGroupNode indexPathByAddingIndex:[selected.children count]];
[entityCollectionTreeController insertObject:newNode atArrangedObjectIndexPath:insertAt];
Now I'd like to open the table column for edit so the user can name the new item. This seems logical:
NSInteger row = [entityCollectionOutlineView rowForItem:newNode];
[entityCollectionOutlineView editColumn:0 row:row withEvent:nil select:YES];
However, row is always -1 indicating the object isn't found. Poking around reveals that the tree controller is not actually putting my objects directly in the tree, but is wrapping them in a node object of its own.
Anyone have insight into how I would go about getting a row index relative to the outline view, so I can do this (without, hopefully, enumerating everything in the outline view and figuring out the mapping back to my node?)
When you insert a new node it gets selected automatically in the outline view. So you can get the proper row by using...
NSInteger selectedRow = [entityCollectionOutlineView selectedRow];
I have a NSTableView with 3 columns. I can sort using any of the colunms by clicking on the column header. Here is my issue though: I am presorting the array and then loading it in NSTableView so the initial TableView is already sorted by the values in one of the 3 columns. Now when I click on the column headers, i can resort and the small ascending/descending image ( triangle) appears indicating how the sort order is. What I want is, to be able to display this ascendingimage triangle in the column header right at the start when the NSTableView is loaded the first time, for the column based on which I have already sorted the Array. Thanks in advance :)
Thanks Peter and cb160. So I added this in my refreshList method:
The *lastColumn parameter has the right identifier if i display it using NSLog, but still that triangle image does not get loaded when the table loads data the first time. Is there something I am missing here ?
My table View is setup like this:
-(IBAction)refreshList:(id)sender
{
//setup array here and sort the array based on one column. This column has
identifier 'col1' and it works as expected
// trying to set the sortindicator image for the column here
NSTableColumn *lastColumn;
lastColumn = [aTableView tableColumnWithIdentifier:#"col1"];
[aTableView setIndicatorImage:[NSImage imageNamed:#"NSDescendingSortIndicator"]
inTableColumn:lastColumn];
[aTable reloadData];
}
- (int) numberOfRowsInTableView:(NSTableView *)aTable
{ // return count of array
}
- (id)tableView:(NSTableView *)aTable objectValueForTableColumn: (NSTableColumn *)
tableColumn row:(int)row
{
//set up arrays here to load data in each column
}
- (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray
*)oldDescriptors
{
//sort here when column headers are clicked
}
You can use the -setIndicatorImage:inTableColumn: method on NSTableView to do this.
You can use the strings NSAscendingSortIndicator and NSDescendingSortIndicator to use the built in images (with UIImage's +imageNamed: method.)
You can get the NSTableColumn * parameter for this method using the -tableColumnWithIdentifier: method on NSTableView. Set the idenitfier in the column using the identifier attribute in Interface Builder (see below)