iPhone : How to create expanadable table view?

会有一股神秘感。 提交于 2020-01-06 19:34:07

问题


I want to create expandable table view.

I found one link which is same as I want.

But I want to create my own table view (Dont want to implements this github code).

How do I achieve this type of functionality ?


回答1:


See this nice tutorial: Table View Animations and Gestures

Demonstrates how you can use animated updates to open and close sections of a table view for viewing, where each section represents a play, and each row contains a quotation from the play. It also uses gesture recognizers to respond to user input: * A UITapGestureRecognizer to allow tapping on the section headers to expand the section; * A UIPinchGestureRecognizer to allow dynamic changes to the height of table view rows; and * A UILongPressGestureRecognizer to allow press-and-hold on table view cells to initiate an email of the quotation.




回答2:


I came across similar feature, to build it a rough algorithm will be:

  1. implement the uitableviewdelgate and uitableviewdatasource protocols

  2. create a global variable expandedSectionIndex = -1;

    = -1 represents all collapsed.

    = 0 represents expandedSectionIndex.

    //the following protocol definitions will take care of which section is to be expanded.
    
     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
      {
          if(expandedSectionIndex == section) 
               return [self.dataArray[section] count];
          else 
                return 0;
       }
    
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
      {
         if(self.dataArray)
                return [self.dataArray count]; 
      }
    

    define custom header views in – tableView:viewForHeaderInSection:

    • buttons having frame equivalent to header view frame
    • set button tag property with value of section number.
    • associate all buttons with selector - (void)expand:(id) sender;

      - (void)expand:(id) sender
       {
          expandedSectionIndex = [sender tag];
          [self.tableView reload];
       }
      

      for more detail enter link description here



来源:https://stackoverflow.com/questions/7568450/iphone-how-to-create-expanadable-table-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!