2015-07-21 153 views
0

在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

回答

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

這怎麼可能在迅速 –

+0

讓我把它轉換成迅速 –