简体   繁体   中英

how to create single tableview with two nested tableview cell xibs using iOS storyboard?

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).

在此处输入图片说明

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;
}

You can implement it by using SKSTableview.

for that Please refer the below link:

  1. SKSTableView

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM