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

问题描述:

在UITableView可展开/可折叠部分的大量搜索后,我仍然没有得到正确的答案,我也尝试过但仍然没有任何东西。iOS 8中的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

@johnykumar我认为你仍然没有得到重点。UITableviewcell行应该展开,当部分被点击时,也为多个部分与特定的数组单元格。无需添加/删除。 –

+0

您必须删除模型(和表格)中的行。如果重构设计以创建Collapsible Section类来处理所有细节,并将tableview委托方法转发到这些类 – sylvanaar

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; 
} 
+0

这怎么可能在迅速 –

+0

让我把它转换成迅速 –