UITableView customized for collapse/expand cells in Storyboard - iphone

Does anyone know a tutorial showing how to implement an UITableView that collapses and expands cells (grouping them) in Storyboard?
Something like:
Name (expand/collapse)
John (click to show John's info, new ViewController)
Sam (idem)
Paul (idem)
Sports (expand/collapse)
Basketball
Baseball
Football
Appreciate any help!

You can do it in following ways:
Insert new cells while selecting a cell at specified index path:
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
And then deleting these on next tap:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];
In this way you'll have to update the datasource(i.e your array) and the index path of tap-able cells with no of new additions and removing in table.
Or you can add/remove only cell with a table in it on cell selection.

Related

UITableview reload SectionHeader but not cells in section

I have a tableview where I want to reload (and change the height of) just the sectionHeader but not the cells inside this section. The reason I want to do this is because I use the fade animation on the header, like so:
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0]
withRowAnimation:UITableViewRowAnimationFade];
But at the same time I am adding a cell in that section with
[self.tableView insertRowsAtIndexPaths:
#[[NSIndexPath indexPathForRow:0 inSection:0]]
withRowAnimation:UITableViewRowAnimationRight];
I'd like to be able to keep the UITableViewRowAnimationRight for the cell that is being added, but when I reload the Section, the fade animation gets applied to the whole section. Is there any way to just reload the sectionHeader, and not the cells in the section?
Just create 2 separate brackets of
[tableView beginUpdates];
[tableView endUpdates];
for the two insertions / changes.

Animate the newly added row in the table

Is it possible to get IndexPath currently inserting new row into a table. The table data is loaded through the Core Data. The datas are sorted through the sections.
I would like to make a new added row to the table to be animated as shown in the following picture link.
http://i45.tinypic.com/2ykjj7n.png
You can use following method:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
The code is tested, and will definitely work for you...
What about this? Is this what you want to gather? I am not sure.
[yourTableView beginUpdates];
[yourTableView moveRowAtIndex:firstStateIndex toIndex:endStateIndex];
[yourTableView endUpdates];
Of course you have to insert the specific row at first so that you can move it afterwards. The animation could happen like this.
[tblMainView beginUpdates];
[tblMainView reloadRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:UITableViewRowAnimationLeft];
[tblMainView endUpdates];

Track the currently selected indexpath and the previously selected indexpath

I want to insert a cell below the currently selected row when tapped once, and delete the cell when tapped again.
I also want to delete the inserted cell from the previous selected row if the user taps a new row.
To achieve this i think i need to track the currently selected indexpath and the previously selected indexpath, though i don't know how to actually achieve this.
This is what i have so far:
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
// Return whether the cell at the specified index path is selected or not
NSNumber *selectedIndex = [self.theSelectedIndexes objectForKey:indexPath];
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}
In didSelectRowAtIndexPath:
// I have a method that tracks if a cell is selected and if its deselected. This is what i'm using to insert a cell when selected and remove it when deselected.
if ([self cellIsSelected:indexPath]) {
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}else {
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
in cellForRowAtIndexPath:
// I use the same method to insert a new cell if the cell is selected or deselected
if [self cellIsSelected:indexPath]{
// allocate new cell below selected cell
else
// allocate standard view cell
At this point it kinda works; when i select a cell, i insert a new cell in its place, and when i tap it again, it reverts back to the regular cell.
I however get into problems when i start selecting other rows after just selecting the previous cell once.
I don't know if I'm doing this correctly, I'm learning as i go.
I thought i would ask the guys here to help me out.
Could you guys help me and provide an example of how i could do this if its no trouble, so i can understand how to do it.
Thanks!
to track last selected path
NSIndexPath *lastIndex = [table indexPathForSelectedRow];
and to compare last index with current index
if(([indexPath compare:lastIndex] == NSOrderedSame)

Shopping list, moving row to the top when selected

I am devleoping a shopping list with all the unbought items to be on the top of the UITable and all the bought items to be on the bottom. The items are read from sqlite DB When the user first clicks on a shopping list item, it means the user has bought the item. Hence i change the label to grey color and move the cell to the bottom,correspondingly in the data source as well. (i have done this by deleting the item and inserting)
However when the user clicks on it again (meaning they need to buy this again), i want the cell to move to the top. How do i do that? How do i update the datasource ?
Here is my code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Item *item=[[appDelegate.slist objectAtIndex:indexPath.row]retain];
NSUInteger index=indexPath.row;
NSUInteger lastindex=appDelegate.slist.count-1;
//get the cell
UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
if (item.iNeed==1) {
cell.textLabel.textColor=[UIColor lightGrayColor];
if (index!=lastindex) {
[appDelegate deleteSLItem:item]; //delete from DB
item.iNeed=0;
[appDelegate insertSLItem:item]; //insert again
//for animation
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:lastindex inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
}
}else if (item.iNeed==0) {
item.iNeed=1;
cell.textLabel.textColor=[UIColor blackColor];
/*
i want to know what am i supposed to do here to move the cell to the top and
correspondingly in the data source as well
*/
[appDelegate updateSLItem:item];
}
[item release];
}
You shouldn't delete and add the item.
Add a bool attribute "bought" to the model.
When displaying the cells make sure to sort your array starting with the ones with bought = YES;
When you click a cell change the bought=!bought
Do reloadData on the table
EDIT: if you want to have the bought ones in the correct order, instead of a bool make "bought" an int and store it's order index

UITableView Detect Selected Cell?

I have multiple UITableViews in my app, is there a method of detecting which cell/row the user has selected in that column?
Also is it possible to programatically deselect a cell/row?
Thanks.
Get currently selected index path for a table:
NSIndexPath *path = [tableView indexPathForSelectedRow];
Deselect currently selected row:
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
These are all easily found in the documentation for UITableView. Perhaps you should look there first next time.
Here's even a link: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
NSIndexPath *path = [tableView indexPathForSelectedRow];
The above line will throw EXC BAD ACCESS if no sell is selected so keep track of your selected cell with NSIndexPath instance variable and only deselect when it is actually selected with isSelected property of cell.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:someIndexPath];
if(cell.isSelected) {
[tableView deselectRowAtIndexPath:someIndexPath animated:YES];
}