当我点按红色按钮
时,我希望折叠tableview单元格- (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
它可以工作,但是tableview单元格移动到另一个标题视图下而不是隐藏。
欢迎任何评论
答案 0 :(得分:11)
查看以下关于UITableview
折叠功能的链接答案 1 :(得分:1)
这可能是您正在寻找的那种动画:
http://www.cocoacontrols.com/platforms/ios/controls/mpfoldtransition
你可能需要稍微调整一下,但如果你喜欢它的样子,这是值得的。
答案 2 :(得分: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
答案 3 :(得分:0)
我使用第一个例子:https://github.com/floriankrueger/iOS-Examples--UITableView-Combo-Box/zipball/master 这很简单。这是一个摘要
选择第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];
答案 4 :(得分:0)
我使用以下方法在Swift中实现了折叠列表。折叠一个部分时,我仍然显示一行(标题),因此对于部分行计数总是至少返回1。这意味着当折叠时,一个部分不会消失。折叠状态更改时,我们使用tableView.reloadSections更新表格的相关部分(带动画)
对于此示例,我在下面的代码中使用以下变量:
sectionHeadings []:一个章节标题数组
sectionVisible []:一个Bool标志数组,指示该部分是否折叠
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中提供单元格时,您可以根据索引值返回标题行或子标题行。 Index = 0是标题,Index = n + 1是第n个子标题
答案 5 :(得分:0)
在Swift 2中这样做非常简单(尽管它对Swift来说并不是唯一的;也可以在Obj-C中轻松完成):
extension UITableView {
public func indexPathsForRowsInSection(section: Int) -> [NSIndexPath]? {
return (0..<self.numberOfRowsInSection(section)).map{NSIndexPath(forRow: $0, inSection: section)}
}
}
假设sender
是UISwitch
,但您可以使用任何Bool值执行此操作:
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)
}