【发布时间】:2015-01-01 07:50:16
【问题描述】:
我能够成功地展开和折叠 tableView 部分,但是到目前为止我无法对单个部分进行此操作。所以所有部分同时折叠或展开,这是因为我调用 [tableView reloadData] 。所以如何展开或折叠特定部分?
这是我目前的做法。
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
headerLabel = [[UILabel alloc]init];
headerLabel.tag = section;
headerLabel.userInteractionEnabled = YES;
headerLabel.backgroundColor = [[UIColor grayColor]colorWithAlphaComponent:0.2];
headerLabel.text = [menuCategoryArray objectAtIndex:section];
headerLabel.frame = CGRectMake(5, 0, tableView.tableHeaderView.frame.size.width, tableView.tableHeaderView.frame.size.height);
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerClicked:)];
tapGesture.cancelsTouchesInView = NO;
[headerLabel addGestureRecognizer:tapGesture];
return headerLabel;
}
-(void)headerClicked:(UIGestureRecognizer*)sender
{
if (!isShowingList) {
isShowingList=YES;
[self.menuTableView reloadData];
UILabel *lbl = (UILabel*)sender.view;
NSLog(@"header no : %d", lbl.tag);
}else{
isShowingList=NO;
[self.menuTableView reloadData];
UILabel *lbl = (UILabel*)sender.view;
NSLog(@"header no : %d", lbl.tag);
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (isShowingList) {
return [[[[jsonArray valueForKey:@"menus"] valueForKey:@"menuName"] objectAtIndex:section] count];
}else{
return 0;
}
return 0;
}
【问题讨论】:
-
扩展使用 insertRowsAtIndexPaths:withRowAnimation: 和折叠使用 deleteRowsAtIndexPaths:withRowAnimation:
-
所以应该在headerClicked方法ri8中使用?那么如何获取indexPaths数组,用于插入?
-
将
isShowingList转换为一个数组(或者可能是一个可变数组,如果您有可变数量的部分)。然后使用section作为数组的索引来确定每个部分是否应该展开。您可以通过测试手势识别器视图的标签来确定哪个节标题被点击。 -
非常感谢您用一些示例代码进行解释,帮助我更好地理解:-]
-
给我一个小时,我会给你我的代码
标签: ios objective-c uitableview uigesturerecognizer