簡體   English   中英

當tableview中有多個節時,展開/折疊表行

[英]Expand/Collapse table row when multiple number of sections in tableview

在這里我正在嘗試創建擴展/折疊表row.It只在1部分中使用didSelectRowAtIndexPath中的此代碼正常工作:

if (selectedIndex == indexPath.row) {
            selectedIndex = -1;
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]withRowAnimation:UITableViewRowAnimationFade];
            return;
        }

        //for table view collapse
        if (selectedIndex != -1) {
            NSIndexPath *prePath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
            selectedIndex = (int)indexPath.row;
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:prePath, nil]withRowAnimation:UITableViewRowAnimationFade];
        }

        //for non selection
        selectedIndex = (int)indexPath.row;
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]withRowAnimation:UITableViewRowAnimationFade];

通過此代碼,我可以展開和折疊表格行,但是當且僅當表格中的1個部分可使用,但當有多個部分出現時,它將展開每個部分的特定行。因此,當我單擊第0部分的第1行時,它將打開所有部分的第1行行。 如何擺脫這種情況?

點擊特定部分的按鈕,您可以更改行數。

在剖面視圖中,您為所有剖面添加了一個按鈕,點擊時具有常用方法。

在攻絲方法中,您只需要更改特定部分的布爾變量值

    ex.. if sender tag you initials with section number. 
    So For Section 0 button is taped then in tapping method
     if sender.tag == 0  
   { 
       if section0tag
       {
        section0tag = false
       }
       else
       {
        section0tag = true 
       }
      table.reload() ..//Which call number of rows at section data source method
  }

 For number of rowsatSection method data source method

 if section == 0
   {
      if section0tag
        {
            return 5 //Expanding rows
        }
     else
       {
            return  0 //collapsing rows
       }
   }   

嘗試此方法,只需重新加載tableview(即rOne,rTwo等是bool值):

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return rOne ? oneRoundType.count : 0 ;
    }else if (section ==1) {
        return rTwo ? twoRoundType.count : 0 ;
    }else if (section ==2) {
        return rThree ? threeRoundType.count : 0 ;
    }else if (section ==3) {
        return rFour ? fourRoundType.count : 0 ;
    }else if (section ==4) {
        return rFive ? fiveRoundType.count : 0 ;
    }
    return 0;
}

請嘗試以下代碼:

sectionIndexToBeExpanded的全局聲明為int,以檢查是否已擴展

int sectionIndexToBeExpanded;

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

   //Assign value 100 to sectionIndexToBeExpanded, if you do not want row expanded 
    sectionIndexToBeExpanded = 100;
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (sectionIndexToBeExpanded==100)  //If not expanded
    {
        return 1;
    }
    else if (sectionIndexToBeExpanded==section) //If section expand add one more row
    {
        return 2;
    }
    else
        return 1;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row==0)
    {
        if (sectionIndexToBeExpanded==100)    //If section not expanded set value of sectionIndexToBeExpanded to 100
        {
            UITableViewCell *cell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];

            [tableView beginUpdates];

            [tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationRight];
            sectionIndexToBeExpanded=(int)indexPath.section;

            [tableView endUpdates];

        }
        else if (sectionIndexToBeExpanded==indexPath.section)   // If Section is already expanded at indexpath then collapse ie. delete row
        {
            UITableViewCell *cell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];

             [tableView beginUpdates];

            [tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row+1 inSection:sectionIndexToBeExpanded]] withRowAnimation:UITableViewRowAnimationLeft];
            sectionIndexToBeExpanded=100;  //Set section to not expanded

            [tableView endUpdates];
        }
        else   //Expand section
        {
            UITableViewCell *cell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
            [tableView beginUpdates];

            [tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row+1 inSection:sectionIndexToBeExpanded]] withRowAnimation:UITableViewRowAnimationLeft];

            //set value of expanded section to sectionIndexToBeExpanded
            sectionIndexToBeExpanded=(int)indexPath.section;

            [tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationLeft];

            [tableView endUpdates];

        }

    }

}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM