2012-05-24 83 views
2

我希望到的tableview細胞倍,當我點擊紅色按鈕如何摺疊/摺疊UITableView的部分?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if([[appDelegate.gHomeHeaderArray_AppDelegate objectAtIndex:section] getIsFolded]==true)  
    {  
     return 0;   
    } 
    else 
    { 
     return 1;  
    } 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if([[appDelegate.gHomeHeaderArray_AppDelegate objectAtIndex:section] getIsFolded ]==true) 
    { 
     return 0; 

    } 
    else 
    { 
     return 1; 
    } 
} 

-(void)buttonTapped:(id)sender; 
{ 
    if([[appDelegate.gHomeHeaderArray_AppDelegate objectAtIndex:v] getIsFolded]) 
    { 
     [[appDelegate.gHomeHeaderArray_AppDelegate objectAtIndex:v] setIsFolded:false]; 
    } 
    else 
    { 
     [[appDelegate.gHomeHeaderArray_AppDelegate objectAtIndex:v] setIsFolded:true]; 
    } 
    [listview reloadData]; 
} 

當我帶的按鈕,它會檢查是否設置numberOfRowsInSection 0

它的工作原理,但實現代碼如下細胞移動在另一個標題視圖下而不是隱藏。

歡迎任何評論

回答

0
//use this method to reload the table, 
    - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 
    //Pass list of indexpaths need to be reloaded, and pass UITableViewRowAnimationBottom this parameter 
0

我使用第一個例子:https://github.com/floriankrueger/iOS-Examples--UITableView-Combo-Box/zipball/master 它很簡單。這裏是一個總結

  1. 保存部分開啓或關閉狀態某處
  2. 當行0是選擇,添加或刪除與動畫行

    NSMutableArray *indexPathArray = [[NSMutableArray alloc] init]; 
    for (int i = 1; i < 20; i++) { 
        NSIndexPath *path0 = [NSIndexPath indexPathForRow:[indexPath row]+i inSection:[indexPath section]]; 
        [indexPathArray addObject:path0]; 
    } 
    [self.tableView beginUpdates]; 
    [self.tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop]; 
    //[self.tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop]; 
    [self.tableView endUpdates]; 
    
  3. 獲得所選擇的小區和切換下拉箭頭
  4. 取消選定細胞
0

我使用FOLL實現在夫特摺疊列表欠款方式。當一個部分被摺疊時,我仍然顯示一行(標題),因此總是返回至少1的部分行數。這意味着摺疊時一段不會消失。當摺疊狀態的變化,我們使用tableView.reloadSections更新表的相關部分(與動畫)

在這個例子中我使用以下變量在下面的代碼:

sectionHeadings []:部分的陣列標題

sectionVisible []:布爾標誌指示部是否被摺疊

sectionSubheadings [] []的數組:用於每個部分時的剖面展開時應該顯示的副標題的陣列。

爲的tableView的有關方法的計算方式如下:

首先返回基於我們的節標題陣列部分的數量。

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // Return the number of sections. 
    return sectionHeadings.count 
} 

我們根據是否摺疊來決定節的行數。如果摺疊那麼我們仍然顯示一個細胞 - 這是節頭,它總是可見:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // Return the number of rows in the section. 
    if sectionVisible[section]{ 
     return sectionSubheadings[section].count + 1 
    }else{ 
     return 1 
    } 
} 

那麼當我們選擇一個單元格,我們查看當前狀態,並打開或關閉當前部分。同樣的行爲也可以附加到按鈕上。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    // if we are selecting a sub heading then this means we want to actually process the click 
    // if we are selecting a section heading (index 0) then we want to open or close the section 
    if indexPath.row == 0{ 

     // this is a section heading 

     // if the section is already open then we don't want to reopen it 
     let bOpening = !sectionVisible[indexPath.section] 

     // bunch all our updates together 
     tableView.beginUpdates() 

     // need to close any existing open sections 
     // NB this is optional behaviour and ensures there is only one section open at a time 
     for (var i:Int = 0;i<sectionVisible.count;i++){ 
      if sectionVisible[i]{ 
       sectionVisible[i] = false 
       tableView.reloadSections(NSIndexSet(index: i), withRowAnimation: UITableViewRowAnimation.Automatic) 
      } 
     } 

     if bOpening{ 
      // open this chapter 
      sectionVisible[indexPath.section] = true 
      tableView.reloadSections(NSIndexSet(index:indexPath.section), withRowAnimation: UITableViewRowAnimation.Automatic) 
     } 

     tableView.endUpdates() 

    }else{ 

     // we have clicked a visible sub heading so process this as required 
    } 
} 

當的tableView提供細胞:的cellForRowAtIndexPath然後可以返回一個標題行或者根據索引值的副標題行。指數= 0是標題,指數= N + 1爲第n個副標題

0

真正簡單的斯威夫特2這樣的方式(儘管它沒有什麼獨特的斯威夫特,可以很容易地的OBJ-C做太):

extension UITableView { 
    public func indexPathsForRowsInSection(section: Int) -> [NSIndexPath]? { 
     return (0..<self.numberOfRowsInSection(section)).map{NSIndexPath(forRow: $0, inSection: section)} 
    } 
} 

假設senderUISwitch,儘管你可以用任何布爾值做到這一點:

if let indexPaths = remindersTableView.indexPathsForRowsInSection(section) where !sender.on { 
    tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Middle) 
} else if let indexPaths: [NSIndexPath] = (0..<numberOfRowsYouWantToAdd).map({NSIndexPath(forRow: $0, inSection: section)}) where sender.on { 
    tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Middle) 
}