2017-03-09 25 views
0

我正試圖在UITableview中實現展開 - 摺疊功能。uibutton不會在uitableview中更改展開 - 摺疊功能的圖像objective-c

1)部分1: 含有2行

2)部分2: 包含3排

1)部分3: 含有2行

2)部分4: 含有2排

  • 科有UILabel一個d UIButton在他們看來。 UIButton有+的圖像和 - 時的「+」按鈕,用戶點擊該節得到了擴大和顯示的行和其他部分得到崩潰 和按鈕改變的圖像「 - 」

    當用戶點擊在部分的「 - 」按鈕上。本節得了collpase和按鈕改變形象「+」

但問題是,當我的「+」按鈕,單擊並重新加載UITableview按鈕的圖像不得到改變。 請幫助我解決方案和適當的方式來實現展開和摺疊行。

- (IBAction)BtnSection_ExpandCollapseAction:(UIButton *)sender { 
     sectionselected = sender.tag; // remember which section is tapped 
     if(sender.selected){ 
      Isopen = FALSE; // whether to expand or to collapse 
      sender.selected = false; 
      [sender setImage:[UIImage imageNamed:@"BtnPlus"] forState:UIControlStateNormal]; 
     }else{ 
      Isopen = TRUE; 
      sender.selected = TRUE; 
      [sender setImage:[UIImage imageNamed:@"btnMinus"] forState:UIControlStateNormal]; 

     } 
     [self.tblView reloadData]; 
    } 

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

     UIView *HeaderView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, self.view.frame.size.width-10, 35)]; 
     HeaderView.backgroundColor = [UIColor lightGrayColor]; 

     UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-80, 5, 50, HeaderView.frame.size.height-10)]; 
     [btn setImage:[UIImage imageNamed:@"BtnPlus"] forState:UIControlStateNormal]; 
     [btn addTarget:self 
        action:@selector(BtnSection_ExpandCollapseAction:) 
     forControlEvents:UIControlEventTouchUpInside]; 

     btn.tag = section; 
     UILabel *LblHeader = [[UILabel alloc] initWithFrame:CGRectMake(20, 5,HeaderView.frame.size.width-10, HeaderView.frame.size.height-10)]; 
     if(section == 0){ 
      LblHeader.text = @"Chief Compalain"; 
     }else if(section == 1){ 
      LblHeader.text = @"Extra Oral"; 
     }else if(section == 2){ 
      LblHeader.text = @"Intra Oral"; 
     }else if(section == 3){ 
      LblHeader.text = @"Radiographical"; 
     } 
     LblHeader.textColor = [UIColor whiteColor]; 

     [HeaderView addSubview:LblHeader]; 

     [HeaderView addSubview:btn]; 

     return HeaderView; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     if(Isopen){ 
      if(section == sectionselected){ 
       return 7; 
      }else{ 
       return 0; 
      } 
     }else{ 
      return 0; 
     } 

    } 


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

} 

參考支票影像 enter image description here

回答

1

這是因爲你設置的圖像後重新加載表視圖。如果您在設置按鈕圖像後不重新加載表格視圖,則會看到圖像實際上會改變。

爲什麼重新加載TableView會導致圖像變回?

這是因爲該按鈕然後在您的viewForHeaderInSection方法中重新初始化,使其回到正值。

執行此代碼是在這裏:

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-80, 5, 50, HeaderView.frame.size.height-10)]; 

[btn setImage:[UIImage imageNamed:@"BtnPlus"] forState:UIControlStateNormal]; 

我該如何解決這個問題?

當按鈕被選中時,動作應該改變數據源對象的一個​​屬性,所以你知道它的打開,例如一個名爲isOpen的屬性。所以當按下按鈕時,您希望section.isOpen爲真。然後重新加載表格視圖。

現在魔術會發生在viewForHeaderInSection。你需要在那裏添加一個if語句。

if (section.isOpen == true) 
{ 
    [btn setImage:[UIImage imageNamed:@"BtnMinus"] forState:UIControlStateNormal]; 
} 
else 
{ 
    [btn setImage:[UIImage imageNamed:@"BtnPlus"] forState:UIControlStateNormal]; 
}