2012-05-24 82 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) 
}