Expand UITableView cell in UITableViewstylePlain

前端 未结 7 2048
北荒
北荒 2020-12-13 16:20

I want to expand tableview cell on it\'s click. There is a tableview in which two cells are expandable and others are not. I need when i click on expandable cell it should s

相关标签:
7条回答
  • 2020-12-13 16:37

    I think What you want is already given by Apple. You can take a look at Sample Code provided by Apple under the title of Table View Animations and Gestures.

    0 讨论(0)
  • 2020-12-13 16:42

    I have given the example source code below. You can customize this example based on your requirement.

    Source Code Link : Click here

    Output Screen:

    enter image description here

    0 讨论(0)
  • 2020-12-13 16:43

    JKExpandTableView is MIT-licensed iOS library that implements the expandable table view. Be sure to checkout the included sample project as well.

    screenshot

    0 讨论(0)
  • 2020-12-13 16:44

    Okay, what I'm thinking is, The titles, Events, Overhead and Friends would be the custom UIView with UIImageView for background, UILabel for title, and UIButton for expand. So basically

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    

    having return count of the titles you've.

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    

    having return UIView for each titles.

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    

    having count of the rows within that title. You may need to maintain an array for this.

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    

    having height of the cell item

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    

    initially it should be 0 (zero) for all sections, when user tap on expand, it should be increase with respect to number of rows * cell height within that section.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    You may need some good logic for setting all rows for expansion

    Your expansion buttons actions something like,

    - (void) expandSection:(UIButton *)sender;
    

    that you can identify which section to be expand using sender.tag, so don't forget to add tag properly. You may need an int in .h file for store the current section, and can be use in - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section datasource method.

    0 讨论(0)
  • 2020-12-13 16:50

    Here is the complete Tutorial with Expandable UITableView

    Here’s the code snip for that.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([self tableView:tableView canCollapseSection:indexPath.section])
        {
            if (!indexPath.row)
            {
                // only first row toggles exapand/collapse
                [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
                NSInteger section = indexPath.section;
                BOOL currentlyExpanded = [expandedSections containsIndex:section];
                NSInteger rows;
    
                NSMutableArray *tmpArray = [NSMutableArray array];
    
                if (currentlyExpanded)
                {
                    rows = [self tableView:tableView numberOfRowsInSection:section];
                    [expandedSections removeIndex:section];
    
                }
                else
                {
                    [expandedSections addIndex:section];
                    rows = [self tableView:tableView numberOfRowsInSection:section];
                }
    
                for (int i=1; i<rows; i++)
                {
                    NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i 
                                                                   inSection:section];
                    [tmpArray addObject:tmpIndexPath];
                }
    
                UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
                if (currentlyExpanded)
                {
                    [tableView deleteRowsAtIndexPaths:tmpArray 
                                     withRowAnimation:UITableViewRowAnimationTop];
    
                    cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
    
                }
                else
                {
                    [tableView insertRowsAtIndexPaths:tmpArray 
                                     withRowAnimation:UITableViewRowAnimationTop];
                    cell.accessoryView =  [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
    
                }
            }
        }
    }
    

    as shown below picture

    enter image description here

    This and this one solution also help you to understand how to manage Expandable TableView

    0 讨论(0)
  • 2020-12-13 16:50

    This is possible by the delegate

    - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
           //write code here
        }
    
    0 讨论(0)
提交回复
热议问题