1

I am trying to implement accordion tableview with parent and child custom tableview cells. I am using below mentioned open source code.

Source code : https://github.com/singhson/Expandable-Collapsable-TableView

In that code having single tableview with single tableview cell. It will show for parent and child cells but I want to make:

  1. Main storyboard Tableview
  2. Parenttableviewcell separate class and xib
  3. Childtableviewcell separate class and xib

It should apply on main controller tableview with accordion. Right now in this code there is no separate custom cells (parent and child using same tableview cell and changing data only).

enter image description here

Apple_Ajay
  • 227
  • 4
  • 17
  • can u give a picture or something that explain what u wanted to achieve?, there's nothing such as nested tableview cell, maybe what u want is add another tableviewcontroller inside each cell? – Tj3n Dec 10 '15 at 05:16
  • I will add. Give me two min!@Tj3n – Apple_Ajay Dec 10 '15 at 05:17
  • Please look above I think now U can understand @Tj3n – Apple_Ajay Dec 10 '15 at 05:55
  • try this [SO Question](http://stackoverflow.com/questions/1938921/expand-collapse-section-in-uitableview), it have many great answer, u should follow the 2nd answer though, even Apple got [this tutorial](https://developer.apple.com/library/ios/samplecode/TableViewUpdates/Introduction/Intro.html) on how to achieve this – Tj3n Dec 10 '15 at 06:11
  • There evrything not Xib's. Its complex for me. anything easy way to do that! – Apple_Ajay Dec 10 '15 at 06:26
  • Why would u want to use xib? u can just drag and drop a cell into the tableViewController in the storyboard and customize it, its very same to xib and don't even need add any init code, its easy in UI (just put a customize cell as header) but the hard part is that u have to split your data into many section and row – Tj3n Dec 10 '15 at 06:57
  • Yeah that is great Idea i agree. Btw how can I merge colable view process into that code? – Apple_Ajay Dec 10 '15 at 07:03

3 Answers3

1

You can implement it by trying to manage the sections and rows of a table view, thats what i assume to be the simplest way to implement it without using any third party code. For example

  • All the section headers will contain their own number of rows.
  • Take a array or dictionary or array that will store the state of expanded rows in sections. (Say '1' for expanded state and '0; for collapsed state)
  • If in the initial state, all the UI is expanded then set all the objects '1' in array or dictionary.
  • On tap of individual headers (I am assuming section will be collapsed on click on individual header of section) you can get which section need to be collapsed. Retain this state as '0' for collapsed section rows in the array or dictionary.
  • Reload the table and check in heightForRow delegate method for the '0' entity in your array or dictionary. Wherever you find it to be '0', return height as 0 in the delegate method.
  • Perform the opposite for reverse functionality.

UPDATE FOR CODE

- (IBAction)btnMenuViewTypeTapped:(id)sender{
UIButton *btnSender = (UIButton *)sender;

if(menuViewType == MVTCollapse){
    [btnSender setImage:[UIImage imageNamed:@"MenuCollapse"] forState:UIControlStateNormal];
    menuViewType = MVTExpand;
    for(NSInteger intAtIndex=0; intAtIndex<[mutArrSearchMenuItems count]; intAtIndex++){
        [mutArrSectionOpened replaceObjectAtIndex:intAtIndex withObject:@"1"];
    }
} else{
    [btnSender setImage:[UIImage imageNamed:@"MenuExpand"] forState:UIControlStateNormal];
    menuViewType = MVTCollapse;
    for(NSInteger intAtIndex=0; intAtIndex<[mutArrSearchMenuItems count]; intAtIndex++){
        [mutArrSectionOpened replaceObjectAtIndex:intAtIndex withObject:@"0"];
    }
}
[tblViewRestaurantMenu reloadData];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [mutArrSearchMenuItems count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger intNumOfRow;
    if([mutArrSectionOpened[section] isEqualToString:@"1"]){
        intNumOfRow = [mutArrSearchMenuItems[section][strMenuType] count];
    }
    else{
        intNumOfRow = 0;
    }
    return intNumOfRow;
}
Rahul Mathur
  • 872
  • 1
  • 7
  • 20
  • Buddy thank you so much for your help btw I am new for this. Please provide some code. It will be very helpfull for me. I am struggling from 3 days for this issue.@Rahul Mathur – Apple_Ajay Dec 10 '15 at 05:26
  • Hold on for 10 mins let me edit the answer with code. – Rahul Mathur Dec 10 '15 at 05:27
  • @Apple_Ajay Sorry for the delay, please check the updated code :) – Rahul Mathur Dec 10 '15 at 07:02
  • Bro its confusing. I need to merge two seperate tablview cells and expanding accordion type @Rahul – Apple_Ajay Dec 10 '15 at 07:13
  • 1
    @Apple_Ajay First of all you don't need two separate XIBs to perform your requirement. Go with storyboard. Don't worry about cell merging as what you need to manage is the number of row for that particular section. You only need to play with that method, nothing else. – Rahul Mathur Dec 10 '15 at 12:22
  • Ok thank you. that type of way only now I am following. the plm my play...Its complicated logics for accordion – Apple_Ajay Dec 10 '15 at 12:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97518/discussion-between-rahul-mathur-and-apple-ajay). – Rahul Mathur Dec 10 '15 at 12:39
0

You can implement it by using SKSTableview.

for that Please refer the below link:

  1. SKSTableView
Pallavi Nikumbh
  • 298
  • 4
  • 19
0

Let make two kinds of custom cell.

+Parent cell is tableview sectionHeader

+Child cell is normal cell

/*your datasource like this
 arrData = @[section, section, section ....]

section.name
section.image
section.arrayChild


 arrayChild = @[child, child, child....]
 child.name
 child.image
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return arrData.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    section = arrData[section];
    child = section.child;
    return child.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //Cell is Custom of UITableViewHeaderFooterViewCell
    //load your Parent Cell here

}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //load your custom chill cell here
}
Trung Phan
  • 923
  • 10
  • 18