iOS 8中的UITableView可扩展/可折叠部分,特定行部分不可扩展/可折叠

时间:2015-07-21 05:46:02

标签: ios objective-c uitableview

经过大量的谷歌搜索UITableView可扩展/可折叠部分后,我仍然没有得到正确的答案,我也尝试过,但仍然没有得到任何结果。

enter image description here

当我点击提醒的第一部分时,它会隐藏起来。

enter image description here

所以这是我的问题,以使其正确。我正在开发一个应用程序,我需要在不同的部分展开折叠行。最初我尝试使用单个部分展开折叠行。现在我希望在多个部分发生同样的事情。任何人都可以告诉我我做了什么错误并建议我。

 BOOL expand,collapsed;


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


//Headerview
     myView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 80.0)];

// Add the label
      UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(kSectionTitleLeftMargin,
kSectionTitleTopMargin,tableView.bounds.size.width - kSectionTitleLeftMargin - kSectionTitleRightMargin,30.0 - kSectionTitleTopMargin - kSectionTitleBottomMargin)];

 // do whatever headerLabel configuration you want here
headerLabel.text = [self tableView:tableView titleForHeaderInSection:section];
[myView addSubview:headerLabel];


UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
[myView addGestureRecognizer:headerTapped];

 //up or down arrow depending on the bool

UIImageView *upDownArrow        = [[UIImageView alloc] initWithImage:expand ?  [UIImage imageNamed:@"upArrowBlack"] : [UIImage imageNamed:@"downArrowBlack"]];
upDownArrow.autoresizingMask    = UIViewAutoresizingFlexibleRightMargin;
upDownArrow.frame               = CGRectMake(265.0, 2.5, 25.0, 25.0);
[myView addSubview:upDownArrow];

return myView;
}

#pragma mark - gesture tapped
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
{
 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];

 {
   collapsed       = !collapsed;

//   reload specific section animated

  NSRange range   = NSMakeRange(indexPath.section, 1);
  NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
   [self.tableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];
   [self.tableView reloadData];


}

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
   if (reminders.count!=0)
  {
    return 2;
}
else
{
    return 1;
}
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}


 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
if ((reminders.count!=0) && (section==0))
{

    return reminders.count;
}

else
{
return 15;
}
}

1 个答案:

答案 0 :(得分:0)

Here is the sample for Collapsable Tableview.

method for tab gesture
-(void)sectionButtonTouchUpInside:(UITapGestureRecognizer*) gestureRecognizer{
[self.tableView beginUpdates];
UIView *sender = [gestureRecognizer view];
int section = sender.tag;
bool shouldCollapse = ![_collapsedSections containsObject:@(section)];
if (shouldCollapse) {
    NSArray* indexPaths = [self indexPathsForSection:section withNumberOfRows:[[_arrItems objectAtIndex:section] count]];
    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
    [_collapsedSections addObject:@(section)];
}
else {
    NSArray* indexPaths = [self indexPathsForSection:section withNumberOfRows:[[_arrItems objectAtIndex:section] count]];
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
    [_collapsedSections removeObject:@(section)];
}
[self.tableView endUpdates];
}

-(NSArray*) indexPathsForSection:(int)section withNumberOfRows:(int)numberOfRows {
NSMutableArray* indexPaths = [NSMutableArray new];
for (int i = 0; i < numberOfRows; i++) {
    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:i inSection:section];
    [indexPaths addObject:indexPath];
}
return indexPaths;
}
相关问题