UA-17470720-3

Jump to content


Photo
* * * * * 1 votes

Hiding Section Based on UISwitch Value


  • Please log in to reply
15 replies to this topic

#1 Domele

Domele

    Starting Out

  • STV Lite
  • Pip
  • 2 posts
Reputation: 3
Good

Posted 23 March 2013 - 03:43 PM

Hi.

I have a UISwitch in one section. When this UISwitch is in the off position, I would like the section below it to hide. And if in the on position, the section would be inserted and would appear.

How do I do this?

Thanks in advance.
  • Tarek and Geraldsl like this

#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 23 March 2013 - 04:59 PM

Hi Domele,

To help you and others easily reuse the following solution, I've created an SCSwitchCell subclass that does what you want. Please find the subclass code below:


@interface SCExpandSwitchCell : SCSwitchCell
{
    @private
    BOOL sectionExpanded;
}
@property (nonatomic, strong) SCTableViewSection *sectionToExpand;
@property (nonatomic, readonly) NSUInteger sectionToExpandIndex;
@end

@implementation SCExpandSwitchCell

// overrides superclass
- (void)loadBoundValueIntoControl
{
    [super loadBoundValueIntoControl];
   
    if(!self.sectionToExpand)
	    return;
   
    // Make sure the current state of self.ownerTableViewModel reflects the initial switchControl state
    if(self.switchControl.on)
    {
	    if([self.ownerTableViewModel indexForSection:self.sectionToExpand] == NSNotFound)
	    {
		    [self.ownerTableViewModel insertSection:self.sectionToExpand atIndex:self.sectionToExpandIndex];
		    [self.ownerTableViewModel.tableView reloadData];
	    }
	    sectionExpanded = TRUE;
    }
    else
    {
	    if([self.ownerTableViewModel indexForSection:self.sectionToExpand] != NSNotFound)
	    {
		    [self.ownerTableViewModel removeSectionAtIndex:self.sectionToExpandIndex];
		    [self.ownerTableViewModel.tableView reloadData];
	    }
	    sectionExpanded = FALSE;
    }
}
// overrides superclass
- (void)cellValueChanged
{
    [super cellValueChanged];
   
    if(!self.sectionToExpand)
	    return;
   
    // Update setionToExpand visibility based on the switchControl on value
    if(self.switchControl.on)
    {
	    [self insertSectionToExpand];
    }
    else
    {
	    [self removeSectionToExpand];
    }
}
- (NSUInteger)sectionToExpandIndex
{
    if(!self.sectionToExpand)
	    return NSNotFound;
   
    NSUInteger sectionIndex = [self.ownerTableViewModel indexForSection:self.ownerSection] + 1;
    return sectionIndex;
}
- (void)insertSectionToExpand
{
    if(!self.sectionToExpand || sectionExpanded)
	    return;
   
    // add sectionToExpand to ownerTableViewModel right below ownerSection
    [self.ownerTableViewModel insertSection:self.sectionToExpand atIndex:self.sectionToExpandIndex];
    sectionExpanded = TRUE;
   
    // add sectionToExpand to the table view with animation
    [self.ownerTableViewModel.tableView insertSections:[NSIndexSet indexSetWithIndex:self.sectionToExpandIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (void)removeSectionToExpand
{
    if(!self.sectionToExpand || !sectionExpanded)
	    return;
   
    // remove sectionToExpand from ownerTableViewModel
    [self.ownerTableViewModel removeSectionAtIndex:self.sectionToExpandIndex];
    sectionExpanded = FALSE;
   
    // remove sectionToExpand from table view with animation
    [self.ownerTableViewModel.tableView deleteSections:[NSIndexSet indexSetWithIndex:self.sectionToExpandIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end

And here is how you'd typically use the new SCExpandSwitchCell:

SCTableViewSection *switchSection = [SCTableViewSection section];

SCExpandSwitchCell *switchCell = [SCExpandSwitchCell cellWithText:@"Expand Section"];
SCTableViewSection *sectionToExpand = [SCTableViewSection section];
[sectionToExpand addCell:[SCTableViewCell cellWithText:@"Sample Cell 1"]];
[sectionToExpand addCell:[SCTableViewCell cellWithText:@"Sample Cell 2"]];
switchCell.sectionToExpand = sectionToExpand;
[switchSection addCell:switchCell];

[self.tableViewModel addSection:switchSection];

Finally, this is how to use it if the switch cell is being automatically generated by a property definition:
switchPropertyDefinition.type = SCPropertyTypeSwitch;
switchPropertyDefinition.uiElementClass = [SCExpandSwitchCell class];
switchPropertyDefinition.willConfigure = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
{
  SCExpandSwitchCell *switchCell = (SCExpandSwitchCell *)cell;

  SCTableViewSection *sectionToExpand = [SCTableViewSection section];
  [sectionToExpand addCell:[SCTableViewCell cellWithText:@"Sample Cell 1"]];
  [sectionToExpand addCell:[SCTableViewCell cellWithText:@"Sample Cell 2"]];

  switchCell.sectionToExpand = sectionToExpand;
};

Hope this helps!

Edited by Tarek Sakr, 25 March 2013 - 04:26 PM.
Fixed some bugs.

  • Everett and wizgod like this

#3 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 23 March 2013 - 05:03 PM

Please note that the above code assumes that the switch cell is in a separate section than the section you need to expand. It also assumes that you want to expand the section directly under the cell.

#4 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 26 March 2013 - 04:02 PM

Update: I've updated the above code to additionally support expanding a single cell under the switch cell. You'll find the new code attached to this post. Hope this helps.

Attached Files


  • cometlinear likes this

#5 Domele

Domele

    Starting Out

  • STV Lite
  • Pip
  • 2 posts
Reputation: 3
Good

Posted 26 March 2013 - 10:13 PM

Just want to confirm that the code previously posted that only expanded a section did work. I tested it and it works just as intended.

Edited by Domele, 26 March 2013 - 10:14 PM.

  • Tarek likes this

#6 devia

devia

    Jr. Member

  • STV 3.0 Pro
  • PipPip
  • 20 posts
Reputation: 8
Good

Posted 28 March 2013 - 05:10 PM

Good question, also good answer.
  • Tarek likes this

#7 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 03 May 2013 - 01:51 AM

I needed to expand more than one row within the same section, so i have extended the SCExpandSwitchCell
You can now also pass in an array for cells (just like the original code, the cells need to be rows right after the SCExpandSwitchCell row in the same section)

    SCTableViewSection *formSection = [self.tableViewModel sectionAtIndex:0];
    SCExpandSwitchCell *expandcell = (SCExpandSwitchCell*)[formSection cellAtIndex:0];
    NSArray *cellArray = [NSArray arrayWithObjects:[formSection cellAtIndex:1], [formSection cellAtIndex:2], nil];
    expandcell.cellsToExpand = cellArray;

Attached Files


  • Tarek, sserye and cometlinear like this

P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#8 Everett

Everett

    Ev

  • STV 5.0 Pro
  • PipPipPipPipPip
  • 191 posts
  • LocationMichigan
Reputation: 60
Outstanding

Posted 06 May 2013 - 08:54 PM

Hi Tarek,

quick question for you or anyone for that matter.

 

I'm trying to build off the examples in the SCExpandSwitchCell class you provided, but I'm looking for a slightly different solution. 

I'm trying to create a cell that has all the functionality of SCSelectionCell but can expand and collapse in place similar to the expand/collapse section,  rather then push a detail view.  

 

I want this functionality to be in a cell, rather then a (expanding/collapsing) section so I can easily add it to property definitions and use it in generated detail views. I know I could probably hack in a solution in the detailModelConfig blocks, but I have so many it would be ugly.   

 

Currently I have many selection cells.  They are initially presented in a detail view and then when selected, they push another detail view.  So at that point the user is 3 levels deep with lots of pushing and popping.  I want to keep them at two levels with less pushes.

 

How my needs differ from the switch cell example is basically this:

 - I want to use the image view for a checkmark image (check)

 - I want the cell to act like a regular tableview cell  and select and deselect when tapped (check)

 - I want to expand and collapse a selection section (check)

 - I want the text label to be left justified and detailTextLabel to be right justified like the selection cell 

 - I want to capture the bound value of the selection section and display it on the detailTextLabel of the controlling cell after selection (like a selection cell).

 - I want to replace the accessory view with an up/down image like the section expand/collapse cell.

 

I'm hacking away a solution based on your code but subclassing a SCTableView cell instead in of SCSwitch.  Its not fully functional yet, but I'm getting there slowly.  I think I will get it working, but not totally confident in  my approach.

 

I'm wondering if you would have any suggestions or tips to accomplish this type of functionality.  

 

thanks so much!

Ev



#9 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 08 May 2013 - 10:26 AM

Hi Everett,

 

Just a quick question to make sure I correctly understand this: Does using my own SCExpandCollapseCell and setting its sectionToExpand property to an SCSelectionSection give you a good start to what you want to achieve?



#10 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 08 May 2013 - 01:36 PM

@ozie: Looks like an awesome addition, thanks for sharing! :)



#11 Everett

Everett

    Ev

  • STV 5.0 Pro
  • PipPipPipPipPip
  • 191 posts
  • LocationMichigan
Reputation: 60
Outstanding

Posted 08 May 2013 - 02:41 PM

Hi Tarek,

 

The example and the associated modifications were a good place to start.  However, when I was trying to implement that approach, I realized that expanding the table by adding a section to the table was not the right approach for my needs.  The main reason is that I'm making heavy use of property definitions to drive the detail views of object cells. 

 

When selected, the object cell presents a detail view that is driven by all the property definitions.  The gotcha I ran into using the expanding Switch example and expanding by section in general is that it will always add that new section beneath the current section (unless you can have a section inside of a section?).  So, say you have a list of 8 cells in one section and the 4th cell and 6th cell (rows) are expandable.   When you tap them or tap the switch or touch whatever control causes them to expand, the new section is going to be added at the bottom of the section (after row 8).  That creates a very odd visual effect.  Putting the cell to expand as in its own section (I'd have to change all my cells - a big change) or at the bottom of a section is not really practical.

 

What I really need is an "Expanding Selection Cell" that works like a typical SCSelection cell but without the detail view controller.  Instead of pushing a detail view, just insert and manage new cells below the main selection cell. 

 

So I switched my efforts to build one of those types of cells and I'm just about done with it.   I have a prototype and it's pretty much working, but I could really, really use your help to review my prototype and help me with the logic in a couple of areas - getting the initializer correct and updating the bound object correctly so it is transparent to the framework and everything else continues to work with the cell as expected.

 

I initially tried to subclass and modify the SCSelectionCell but it looks like the methods that create the detail views are private and not easily overridden.  So, I went back to a base SCTableView cell and have added similar functionality (but not all) provided in the SCSelectionCell.  

 

I think when you see it working it will make more sense.  That type of cell would be a very nice and a very practical addition to STV.  In fact, the most elegant way to implement it would be to modify the source of the current SCSelectionCell and add a property like   BOOL inLine;   If it's set to yes expand inside of the current section and if not, push the detail view. 



#12 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 10 May 2013 - 02:24 PM

Of course, I'd love to have a look at your new SCExpandSelectionCell. Would you please post the zipped .h & .m files in a new topic, so that we wouldn't flood this topic with posts that are irrelevant to SCExpandSwitchCell? Thank you.



#13 Laeger

Laeger

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 59 posts
Reputation: 6
Good

Posted 09 December 2013 - 01:48 PM

Tarek,

Your SCExpandSwitchCell worked perfectly for me.  Thank you for putting this together!

-Aaron



#14 Mirko Tomic

Mirko Tomic

    Starting Out

  • STV 3.0 Pro
  • Pip
  • 2 posts
Reputation: 0
Neutral

Posted 08 August 2014 - 07:51 PM

link to ozie attachement not working :\



#15 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 08 August 2014 - 07:57 PM

Hey Mirko.. have to delete things from time to time so i have space to upload other things;)

 

handle with care.. i have been playing with it lately and can’t remember if its 100% bug free or not..added things to remove and replace and other stuff now.. its a bit of a mess now

 

Attached File  SCExpandSwitchCell.zip   5.09K   4 downloads


P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#16 Mirko Tomic

Mirko Tomic

    Starting Out

  • STV 3.0 Pro
  • Pip
  • 2 posts
Reputation: 0
Neutral

Posted 10 August 2014 - 05:45 AM

Thanks! I've seen this kind of field in Do Not Disturb, and thats why I wanted this kind of behaviour.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users