UiTableView cell animated cells are hidden under section view. - ios

Okay so I've added a picture of what's going on and this may help visualize the issue a little better.
So once I click on the "Shopping & Deals" section image a list of items or cells drop down; however, as you can see there are still some cells that are hidden under the "Shopping & Deals" image. Now here's the code:
- (void) sectionOpened : (NSInteger) section{
SectionInfo *array = [self.sectionInfoArray objectAtIndex:section];
array.open = YES;
//Google PageView tracker
self.trackedViewName = [NSString stringWithFormat:#"Find Things To Do -- %#",array.category.name];
//Mobile Apptracking track event
[[MobileAppTracker sharedManager] trackActionForEventIdOrName:[NSString stringWithFormat:#"Find Things To Do -- %#",array.category.name] eventIsId:NO];
NSInteger count = [array.category.list count];
NSMutableArray *indexPathToInsert = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i<count;i++)
{
[indexPathToInsert addObject:[NSIndexPath indexPathForRow:i inSection:section]];
}
NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
NSInteger previousOpenIndex = self.openSectionIndex;
if (previousOpenIndex != NSNotFound)
{
SectionInfo *sectionArray = [self.sectionInfoArray objectAtIndex:previousOpenIndex];
sectionArray.open = NO;
NSInteger counts = [sectionArray.category.list count];
[sectionArray.sectionView toggleButtonPressed:FALSE];
for (NSInteger i = 0; i<counts; i++)
{
[indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:previousOpenIndex]];
}
}
UITableViewRowAnimation insertAnimation;
UITableViewRowAnimation deleteAnimation;
if (previousOpenIndex == NSNotFound || section < previousOpenIndex)
{
insertAnimation = UITableViewRowAnimationTop;
deleteAnimation = UITableViewRowAnimationBottom;
}
else
{
insertAnimation = UITableViewRowAnimationBottom;
deleteAnimation = UITableViewRowAnimationTop;
}
[atableView beginUpdates];
[atableView insertRowsAtIndexPaths:indexPathToInsert withRowAnimation:insertAnimation];
[atableView setContentOffset:CGPointMake(0, self.view.bounds.size.height)];
[atableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:deleteAnimation];
[atableView endUpdates];
self.openSectionIndex = section;
}
Now if you look at the bottom of the code there's this:
[atableView setContentOffset:CGPointMake(0, self.view.bounds.size.height)];
and this offsets the view according the y bounds. The reason for this was because if you look at the picture, once I clicked on the "Productivity" section the cells would drop down past the view and you couldn't tell if any cells had actually populated, so offsetting the content pushed the view up so that those cells would appear. Now the issue is that once I click on "Shopping & Deals" the cells just kind of hang underneath the section. Does anyone have any ideas on how to tackle this one?

Related

Store Cells with switch on from a Tableview

What I want is to generate a sub-contact using properties form an existing CNContact object.
I got the CNContact object from CNContactPickerViewController and display all properties in a tableview.
The tableviewcell is embedded with switch. I have a preview button on the navigation item, when the button is pressed, if the switch in a property is on, this property should be stored in a new CNMutableContact.
My issue is: If the Contact has too many properties, I cannot get the off-screen properties stored. Is there a way to solve this problem.
part of the code to get sub-contact:
+(CNMutableContact*)newContactWithSelectedFieldInTableView:(UITableView*)tableView FromContact:(CNContact*)contact
{
CNMutableContact* aContact = [[CNMutableContact alloc]init];
//get all indexPath from tableview
NSMutableArray* indexPathArr = [[NSMutableArray alloc]init];
NSInteger nSections = [tableView numberOfSections];
for (int j=0; j<nSections; j++) {
NSInteger nRows = [tableView numberOfRowsInSection:j];
for (int i=0; i<nRows; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];
[indexPathArr addObject:indexPath];
}
}
//selected phone numbers
//go through indexPath
for (NSIndexPath* path in indexPathArr)
{
UITableViewCell* nameCell = [tableView cellForRowAtIndexPath:path];
UISwitch *mySwitch = (UISwitch *)nameCell.accessoryView;
switch (path.section)
{
case basicInfoSection://basic info section (name,company,department,title)
{
int row = 0;
if(path.row==row)
{
if(mySwitch.on)
{
aContact.givenName = contact.givenName;
aContact.middleName = contact.middleName;
aContact.familyName = contact.familyName;
}
}
if(![contact.organizationName isEqualToString:#""])
{row += 1;
if(path.row==row)//company row
{
//store company
if(mySwitch.on)
aContact.organizationName = contact.organizationName;
}
}
if(![contact.departmentName isEqualToString:#""])
{row += 1;
if(path.row==row)//department row
{
//store department
if(mySwitch.on)
aContact.departmentName = contact.departmentName;
}
}
if(![contact.jobTitle isEqualToString:#""])
{row += 1;
if(path.row==row)//jobTitle row
{
//store job Title
if(mySwitch.on)
aContact.jobTitle = contact.jobTitle;
}
}
}
break;
case phoneSection:
{
if(mySwitch.on)
{
aContact.phoneNumbers = [aContact.phoneNumbers arrayByAddingObject:contact.phoneNumbers[path.row]];
}
}
break;
I come up with a solution and it work fine for now, some code are listed below:
1# create a subclass of UISwitch
#import <UIKit/UIKit.h>
#interface SwitchWithIndex : UISwitch
#property (strong ,nonatomic) NSIndexPath* indexPath;
#end
2# create a dictionary that record indexPath with switch state and in view did load, loop datasource(the CNContact) for all possible indexPath according to the demand tableview layout.
for(NSInteger section=0; section<7; section++)
{
switch (section) {
{
//section 1 basic info(name, company, department, job title)
case basicInfoSection:
{int row =0;
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[_switchStateAtIndex setObject:boolNumber forKey:indexPath];
if(![_contact.organizationName isEqualToString:#""])
{row += 1;
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[_switchStateAtIndex setObject:boolNumber forKey:indexPath];
}
if(![_contact.departmentName isEqualToString:#""])
{row += 1;
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[_switchStateAtIndex setObject:boolNumber forKey:indexPath];
}
if(![_contact.jobTitle isEqualToString:#""])
{row += 1;
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[_switchStateAtIndex setObject:boolNumber forKey:indexPath];
}
}
break;
//section 2 phones
case phoneSection:
{
for(NSInteger row=0; row<[_contact.phoneNumbers count];row++)
{
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[_switchStateAtIndex setObject:boolNumber forKey:indexPath];
}
}
break;
//more code ..
3# In Table View Datasource Delegate load the state of switch at indexpath and
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
SwitchWithIndex* mySwitch = [[SwitchWithIndex alloc] init];
[mySwitch addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
NSNumber* switchStateNumber =[_switchStateAtIndex objectForKey:indexPath];
BOOL switchState = [switchStateNumber boolValue];
mySwitch.on = switchState;
mySwitch.indexPath = [[NSIndexPath alloc]init];
mySwitch.indexPath = indexPath;
cell.accessoryView = mySwitch;
//more code
and the following code to detect change on switch state.
- (void) switchChanged:(id)sender {
SwitchWithIndex* mySwitch = sender;
NSIndexPath* indexPath = mySwitch.indexPath;
NSLog(#"%#",indexPath);
NSNumber* switchStateBool = [NSNumber numberWithBool:mySwitch.on ? YES : NO];
[_switchStateAtIndex setObject:switchStateBool forKey:indexPath];
NSLog( #"The switch is %#", mySwitch.on ? #"ON" : #"OFF" );
}
4# And finally save selected field based on switch state
+(CNMutableContact*)newContactFrom:(CNContact*)contact withSwitchState:(NSMutableDictionary*)switchState
{
CNMutableContact* aContact = [[CNMutableContact alloc]init];
for (NSIndexPath* indexPath in switchState.keyEnumerator)
{
NSNumber* boolNumber = [switchState objectForKey:indexPath];
BOOL switchOn = [boolNumber boolValue];
switch (indexPath.section) {
case basicInfoSection://basic info section (name,company,department,title)
{
int row = 0;
if(indexPath.row==row)
{
if(switchOn)
{
aContact.givenName = contact.givenName;
aContact.middleName = contact.middleName;
aContact.familyName = contact.familyName;
}
}
if(![contact.organizationName isEqualToString:#""])
{row += 1;
if(indexPath.row==row)//company row
{
//store company
if(switchOn)
aContact.organizationName = contact.organizationName;
}
}
//more code
There may be other solution to solve this issue, above is what I can do for now.
Rather than enumerating all of the cells in a table view, you need to store the state of the switches somewhere other than in the table view's cells. I would imagine that this is already being done, otherwise the state of the switches would not be reliable/consistent just from scrolling the table view.
For instance, in your cellForRowAtIndexPath: method, you must tell the cell whether the switch should be displayed as on or off. You would do this by holding the state of your switches in an instance-level array. Here is one way to do that:
#interface YourClass ()
{
// Create an instance-level array to store switch values.
NSMutableArray *switchValues;
}
...
- (void)viewDidLoad {
switchValues = [NSMutableArray new];
for(int i = 0; i < YOUR_TABLE_SIZE; i++)
[switchValues insertObject:[NSNumber numberWithBool:DEFAULT_SWITCH_STATE]];
// Alternatively, populate your array with another data object if all the
// switches do not have the same default starting state (on/off).
}
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
YourCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier" forIndexPath:indexPath];
...
// Add a target to the switch so we know when it toggles.
[cell.toggleSwitch addTarget:self action:#selector(switchSwitched:) forControlEvents:UIControlEventValueChanged];
cell.toggleSwitch.tag = indexPath.row;
// Set the state to the appropriate value.
cell.toggleSwitch.on = [[switchValues objectAtIndex:indexPath.row] boolValue];
return cell;
}
- (void)switchSwitched:(UISwitch *)switcher
{
// Toggle the switch's value in the instance array
[switchValues replaceObjectAtIndex:switcher.tag withObject:[NSNumber numberWithBool:switcher.on]];
}

Strange behavior - Row won't reselect until three taps

I'm currently facing a strange issue with a UITableView in iOS whereby deselected rows are no longer selectable immediately, you must tap three times before you can select again. I'm implementing something akin to a checklist, with a "clear" button to go through and deselect all the cells. This effect only occurs when the cell had been previously selected, but the "clear" button is pressed. If a cell had been untouched before and is touched after the clear button, it will be presented and toggle fine.
Here is what I have
I should mention that the VC this Table is in is stored inside a container, hence the reference to it.
The deselect
- (IBAction)btnClearTapped:(id)sender {
UITableView *locationList = self.locationsVc.tableView;
for (NSUInteger i = 0; i < [locationList numberOfSections]; i++) {
for (NSUInteger j = 0; j < [locationList numberOfRowsInSection:i]; j++) {
[locationList deselectRowAtIndexPath:[NSIndexPath indexPathForRow:j inSection:i] animated:YES];
}
}
}
The didSelectRowAtIndexPath method
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
BOOL isChecked = !((NSNumber *) cellToggleDict[indexPath]).boolValue;
cellToggleDict[indexPath] = #(isChecked);
UIView *selectionColorView = [[UIView alloc] init];
if (isChecked) {
selectionColorView.backgroundColor = selectedColor;
selectedLocations[indexPath] = [[Search sharedManager] locationAtIndexPath:indexPath];//PFObject
}
else {
selectionColorView.backgroundColor = unselectedColor;
[selectedLocations removeObjectForKey:indexPath];
}
cell.selectedBackgroundView = selectionColorView;
}
And finally the dequeue and redraw method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Reuse" forIndexPath:indexPath];
PFObject *object = [[Search sharedManager] locationAtIndexPath:indexPath];
cell.textLabel.text = object[#"Name"];
BOOL isChecked = ((NSNumber *) cellToggleDict[indexPath]).boolValue;
UIView *selectionColorView = [[UIView alloc] init];
if (isChecked) {
selectionColorView.backgroundColor = selectedColor;
}
else {
selectionColorView.backgroundColor = unselectedColor;
}
cell.selectedBackgroundView = selectionColorView;
return cell;
}
You don't clear the cellToggleDict entries in the btnClearTapped: method. You should have.
- (IBAction)btnClearTapped:(id)sender {
UITableView *locationList = self.locationsVc.tableView;
[cellToggleDict removeAllObjects]; // Clear all objects from the selected store
for (NSUInteger i = 0; i < [locationList numberOfSections]; i++) {
for (NSUInteger j = 0; j < [locationList numberOfRowsInSection:i]; j++) {
[locationList deselectRowAtIndexPath:[NSIndexPath indexPathForRow:j inSection:i] animated:YES];
}
}
}

Find indexPath of cell by label

I have table with cells; each cell contain label with phone number. When i tap on the label, UIMenuController appears.
Is there a main way to find indexPath of cell, which contain selected label? (didSelectRowAtIndexPath: should not be called)
I can imagine many dirty hacks, but i looking for a main/good solution.
upd
I have reference to selected label.
Edit:
This is a better way.
- (IBAction)someMethod:(id)sender {
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
}
From your label, search for the cell object in the label's hierarchy:
-(UITableViewCell*) cellContainingView:(UIView*)view
{
UIView* currentView = view;
while (currentView)
{
if ([currentView isKindOfClass:[UITableViewCell class]])
{
return (UITableViewCell*)currentView;
}
currentView = currentView.superview;
}
return nil;
}
Once you have the cell, call [tableView indexPathForCell:cell]
The use of this method instead of hard coding view.superview.superview makes your code stronger because it handle possible changes in cell's view hierarchy through system's versions.
You could go:
NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [tableView numberOfSections]; ++j)
{
for (NSInteger i = 0; i < [tableView numberOfRowsInSection:j]; ++i)
{
[cells addObject:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
}
}
for (UITableViewCell *cell in cells){
for (UILabel *label in cell.subviews){
if (label == yourLabel)
indexPath = [tableView indexPathForCell: cell];
}
}

Reload section without reloading section header

I have a UITableView, where there is a UISegmentedControl in the header view. It should work exactly like in the App Store app: As the user scrolls, the title in the header scrolls off the screen but the segmentedControl sticks under the navigationBar.
When the user selects a segment, the section below the header should be reloaded with a nice UITableViewRowAnimation. However, as I call tableView:reloadSections:withRowAnimation:, the header view is animated as well, which I want to prevent, because it looks terrible.
Here's my code for this:
- (void)selectedSegmentIndexChanged:(UISegmentedControl *)sender
{
int index = sender.selectedSegmentIndex;
if (index < self.oldIndex) {
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationLeft];
} else if (index > self.oldIndex) {
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationRight];
}
self.oldIndex = index;
}
Anyone has an idea how to reload the section below the header without reloading the header itself?
Maybe you should try with
[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationLeft] //or UITableViewRowAnimationRight
However, I'm not sure but I think it can rise some error in the case where you have less rows to reload than previously.
Edit
I think you could deal with [tableView beginUpdates] and [tableView endUpdates] to solve your problem.
For example, you have 2 arrays of data to display. Let name them oldArray and newArray.
A sample of how what you could do :
- (void)selectedSegmentIndexChanged:(UISegmentedControl *)sender
{
[self.tableView setDataSource: newArray];
int nbRowToDelete = [oldArray count];
int nbRowToInsert = [newArray count];
NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < nbRowToInsert; i++) {
[indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:section]];
}
NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < nbRowToDelete; i++) {
[indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:section]];
}
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
}
If you are using Swift 2.0, feel free to use this extension.
Be warned: passing in the wrong oldCount or newCount will crash you program.
extension UITableView{
func reloadRowsInSection(section: Int, oldCount:Int, newCount: Int){
let maxCount = max(oldCount, newCount)
let minCount = min(oldCount, newCount)
var changed = [NSIndexPath]()
for i in minCount..<maxCount {
let indexPath = NSIndexPath(forRow: i, inSection: section)
changed.append(indexPath)
}
var reload = [NSIndexPath]()
for i in 0..<minCount{
let indexPath = NSIndexPath(forRow: i, inSection: section)
reload.append(indexPath)
}
beginUpdates()
if(newCount > oldCount){
insertRowsAtIndexPaths(changed, withRowAnimation: .Fade)
}else if(oldCount > newCount){
deleteRowsAtIndexPaths(changed, withRowAnimation: .Fade)
}
if(newCount > oldCount || newCount == oldCount){
reloadRowsAtIndexPaths(reload, withRowAnimation: .None)
}
endUpdates()
}
Try this:
BOOL needsReloadHeader = YES;
UIView *oldHeaderView = nil;
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerToReturn = nil;
if(needsReloadHeader == YES) {
headerToReturn = [[UIView alloc] init];
// ...
// custom your header view in this block
// and save
// ...
oldHeaderView = headerToReturn;
} else {
headerToReturn = oldHeaderView;
}
return headerToReturn;
}
Your just need to change 'needsReloadHeader' to 'NO' in other places.
An objective-c version of Intentss extension
#interface UITableView (Extensions)
- (void)reloadRowsInSection:(NSUInteger)sectionIndex withRowAnimation:(UITableViewRowAnimation)rowAnimation oldCount:(NSUInteger)oldCount newCount:(NSUInteger)newCount;
#end
#implementation UITableView (Extensions)
- (void)reloadRowsInSection:(NSUInteger)sectionIndex withRowAnimation:(UITableViewRowAnimation)rowAnimation oldCount:(NSUInteger)oldCount newCount:(NSUInteger)newCount {
NSUInteger minCount = MIN(oldCount, newCount);
NSMutableArray *insert = [NSMutableArray array];
NSMutableArray *delete = [NSMutableArray array];
NSMutableArray *reload = [NSMutableArray array];
for (NSUInteger row = oldCount; row < newCount; row++) {
[insert addObject:[NSIndexPath indexPathForRow:row inSection:sectionIndex]];
}
for (NSUInteger row = newCount; row < oldCount; row++) {
[delete addObject:[NSIndexPath indexPathForRow:row inSection:sectionIndex]];
}
for (NSUInteger row = 0; row < minCount; row++) {
[reload addObject:[NSIndexPath indexPathForRow:row inSection:sectionIndex]];
}
[self beginUpdates];
[self insertRowsAtIndexPaths:insert withRowAnimation:rowAnimation];
[self deleteRowsAtIndexPaths:delete withRowAnimation:rowAnimation];
[self reloadRowsAtIndexPaths:reload withRowAnimation:rowAnimation];
[self endUpdates];
}
#end
You're reloading the section, so clearly everything in the section will be reloaded (including the header).
Why not instead place the UISegmentedControl inside UITableView's tableHeaderView? This would allow for exactly the behavior you're after.
The simple answer is just don't reload the sections animated, just use UITableViewRowAnimationNone.
Right now you're using UITableViewRowAnimationLeft and UITableViewRowAnimationRight, which slides your section in and out as well.
However, even with UITableViewRowAnimationNone, rows will still be animated if the number of cells before the update differ from the ones after the update.
Also, a nice read on this topic, here.
Cheers.
Here's another way which you could use and still use animations.
Let's say you have a dynamic DataSource, which changes when you select something, and you want to update just the rows of that section, while leaving the section header on top, untouched.
/** I get the desired handler from the handler collection. This handler is just a
simple NSObject subclass subscribed to UITableViewDelegate and UITableViewDataSource
protocols. **/
id handler = [self.tableViewHandlers objectForKey:[NSNumber numberWithInteger:index]];
/** Get the rows which will be deleted */
NSInteger numberOfRows = [self.tableView numberOfRowsInSection:sectionIndex];
NSMutableArray* indexPathArray = [NSMutableArray array];
for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++){
[indexPathArray addObject:[NSIndexPath indexPathForRow:rowIndex inSection:sectionIndex]];
}
/** Update the handler */
[self.tableView setDataSource:handler];
[self.tableView setDelegate:handler];
/** Get the rows which will be added */
NSInteger newNumberOfRows = [handler tableView:self.tableView numberOfRowsInSection:sectionIndex];
NSMutableArray* newIndexPathArray = [NSMutableArray array];
for (int rowIndex = 0; rowIndex < newNumberOfRows; rowIndex++){
[newIndexPathArray addObject:[NSIndexPath indexPathForRow:rowIndex inSection:sectionIndex]];
}
/** Perform updates */
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:newIndexPathArray withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
As a note, please stick to the specified order of operations, UITableView demands it.
If you have only one handler (datasource and delegate), it's easy to modify the above code to achieve the same results.

UITableView with open & closed sections

I have a UITableView with several sections.
I would like click on a section to "close/open" its content, in order to show/hide the rows under it. So that, I can keep some sections open (with its rows visible) and others close, with the next section immediately below the previous section header.
How can I implement it? Do I need to subclass the UITableView and add a gesture recognizer and somehow add an animation to the rows? But I'm not sure this is easy...
thanks
Use reloadSections:withRowAnimation to trigger the change;
Provide the updated number of rows in UITableViewDataSource delegate
I can't comment for the question as I don't have enough reputation.
As per my understanding, you want to achieve accordion functionality for your table view. For this, please check this:- effect or animation in UItableVIew and How to implement an accordion view for an iPhone SDK app?
Just subclass sectionHeaderView and define delegate methods like this.
#protocol SectionHeaderViewDelegate <NSObject>
#optional
-(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionOpened:(NSInteger)section;
-(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionClosed:(NSInteger)section;
#end
And then in tableViewCOntroller.h
#interface TableViewController : UITableViewController <SectionHeaderViewDelegate>
And in tableViewCOntroller.m
-(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionOpened:(NSInteger)sectionOpened
{
SectionInfo *sectionInfo = [self.sectionInfoArray objectAtIndex:sectionOpened];
sectionInfo.open = YES;
/*
Create an array containing the index paths of the rows to insert: These correspond to the rows for each quotation in the current section.
*/
NSInteger countOfRowsToInsert = [sectionInfo.play.quotations count];
NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < countOfRowsToInsert; i++) {
[indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:sectionOpened]];
}
/*
Create an array containing the index paths of the rows to delete: These correspond to the rows for each quotation in the previously-open section, if there was one.
*/
NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
NSInteger previousOpenSectionIndex = self.openSectionIndex;
if (previousOpenSectionIndex != NSNotFound) {
SectionInfo *previousOpenSection = [self.sectionInfoArray objectAtIndex:previousOpenSectionIndex];
previousOpenSection.open = NO;
[previousOpenSection.headerView toggleOpenWithUserAction:NO];
NSInteger countOfRowsToDelete = [previousOpenSection.play.quotations count];
for (NSInteger i = 0; i < countOfRowsToDelete; i++) {
[indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:previousOpenSectionIndex]];
}
}
// Style the animation so that there's a smooth flow in either direction.
UITableViewRowAnimation insertAnimation;
UITableViewRowAnimation deleteAnimation;
if (previousOpenSectionIndex == NSNotFound || sectionOpened < previousOpenSectionIndex) {
insertAnimation = UITableViewRowAnimationTop;
deleteAnimation = UITableViewRowAnimationBottom;
}
else {
insertAnimation = UITableViewRowAnimationBottom;
deleteAnimation = UITableViewRowAnimationTop;
}
// Apply the updates.
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:insertAnimation];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:deleteAnimation];
[self.tableView endUpdates];
self.openSectionIndex = sectionOpened;
}
-(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionClosed:(NSInteger)sectionClosed
{
/*
Create an array of the index paths of the rows in the section that was closed, then delete those rows from the table view.
*/
SectionInfo *sectionInfo = [self.sectionInfoArray objectAtIndex:sectionClosed];
sectionInfo.open = NO;
NSInteger countOfRowsToDelete = [self.tableView numberOfRowsInSection:sectionClosed];
if (countOfRowsToDelete > 0)
{
NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < countOfRowsToDelete; i++) {
[indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:sectionClosed]];
}
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationTop];
}
self.openSectionIndex = NSNotFound;
if ([selectedIndexes count]>0)
{
for(NSIndexPath *indexPath in selectedIndexes)
{
if(sectionClosed == indexPath.section)
{
[sectionInfo.headerView changeOnHighlighted:YES];
break;
}
else
{
[sectionInfo.headerView changeOnHighlighted:NO];
}
}
}
else
{
[sectionInfo.headerView changeOnHighlighted:NO];
}
}
And for further details you can refer this sample project from iOS dev lib..
Here is a simple solution by which you can even create a custom expanded/ collapse view.
here are simple step
1) create a custom view add button over it.
///
join all outlet and create on BOOL variable in view class
#property (weak, nonatomic) IBOutlet UIButton *BtnAction;
#property(assign, nonatomic)BOOL isOpen;
// Create a header where tableview is added and you want it.
here is a simple logic to added as much as you need . I have added which are in headertitle array I wanted it to be dynamic .
NSMutableArray * headerTitle = [NSMutableArray arrayWithObjects:#"Your Order", #"Delivery Address", #"Pay By", nil];
for (NSUInteger index = 0; index<headerTitle.count; index++) {
VGOrderHeader* HeaderView = [[[NSBundle mainBundle] loadNibNamed:#"VGOrderHeader" owner:self options:nil] lastObject];
HeaderView.frame = CGRectMake(0, 0, 32, 40);
HeaderView.BtnAction.tag = index;
if (index == 0) {
HeaderView.isOpen = YES;
HeaderView.lblPlus.text = [NSString stringWithFormat:#"open"];
}
[HeaderView.BtnAction addTarget:self action:#selector(selectSectionToOpen:) forControlEvents:UIControlEventTouchUpInside];
[headerArray addObject:HeaderView];
}
/// Here is header click action.
-(void)selectSectionToOpen:(UIButton *)sender{
for (NSUInteger Increment=0; Increment<headerArray.count; Increment++) {
if (sender.tag == Increment) {
DCOrderHeader* HeaderView= headerArray[Increment];
HeaderView.isOpen = !HeaderView.isOpen;
}
}
// little animation
dispatch_async(dispatch_get_main_queue(), ^{
[UIView transitionWithView:self.tableView
duration:0.55f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^(void) {
[self.tableView reloadData];
} completion:NULL];
});
}
/// Finally Assign view in header method of table view and provide a height
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return [headerArray objectAtIndex:section];
}
// Final Touch
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return headerArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
DCOrderHeader* HeaderView = headerArray[section];
if (HeaderView.isOpen == YES) {
return self.someArray.count;
}else{
return 0;
}
}

Resources