2014-10-07 58 views
-1

我现在有在该按钮时每个部分的viewForFooterInSection一个按钮,从0至65的高度的高度增加的行5-10的高度,如下所示:如何在UITableView的每个部分有不同的行高?

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { 
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; 
    view.backgroundColor = [UIColor whiteColor]; 

    self.moreButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    self.moreButton.frame = CGRectMake(0, 0, 320, 44); 
    [self.moreButton setImage:[UIImage imageNamed:@"downarrow.png"] forState:UIControlStateNormal]; 
    [self.moreButton addTarget:self action:@selector(moreButtonSelected:) forControlEvents:UIControlEventTouchUpInside]; 
    [view addSubview:self.moreButton]; 

    return view; 
} 

- (void)moreButtonSelected:(id)sender { 
    if (_hasPressedShowMoreButton == NO){ 
     self.hasPressedShowMoreButton = YES; 
    } 
    else if (_hasPressedShowMoreButton == YES){ 
     self.hasPressedShowMoreButton = NO; 
    } 

    [self.matchCenter reloadData]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (_hasPressedShowMoreButton == YES){ 
     return 65; 
    } 

    else if (_hasPressedShowMoreButton == NO){ 
     if (indexPath.row > 3){ 
      return 0; 
     } 
     else{ 
      return 65; 
     } 
    } 

} 

这就意味着是“显示更多”类型的动作,这样当按下时,其余行将出现,而不是仅前4个。问题是,当按下某个节的页脚中的按钮时,它会展开所有部分到10行,而不仅仅是按下按钮的那一行。我怎样才能指定按钮被按下的部分?

编辑:

我试过以下的建议,并做如下图所示,但它引发的崩溃与错误'NSInvalidArgumentException', reason: '-[UIButton section]: unrecognized selector sent to instance 0x7f94840640b0'

代码:

- (void)moreButtonSelected:(NSIndexPath *)indexPath{ 
    if (_hasPressedShowMoreButton == NO){ 
     self.hasPressedShowMoreButton = YES; 
     indexPath.section == _expandedSection; 
    } 
    else if (_hasPressedShowMoreButton == YES){ 
     self.hasPressedShowMoreButton = NO; 
    } 

    [self.matchCenter reloadData]; 
} 


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (_hasPressedShowMoreButton == YES){ 
     if (indexPath.section == _expandedSection){ 
      return 65; 
     } 

    } 

    else if (_hasPressedShowMoreButton == NO){ 
     if (indexPath.row > 3){ 
      return 0; 
     } 
     else{ 
      return 65; 
     } 
    } 

} 
+0

您不能使用单个法尔'_hasPressedShowMoreButton'。您需要跟踪所有部分的按钮状态。 – rmaddy 2014-10-07 17:30:59

+0

@rmaddy啊,有道理。介绍如何跟踪每个按钮的状态? – Ghobs 2014-10-07 17:33:34

回答

1

您应该检查indexPath.section == _expandedSection。 (将您按下的更多按钮的indexPath.section存储到_expandedSection中。)

扩展UIButton以允许分配给它。

@interface MoreButton : UIButton 
@property (assign) NSInteger sectionIndex; 
@end 

@implementation MoreButton 
@end 

将属性添加到您的班级。

@property (assign) NSInteger expandedSection; 
self.expandedSection = -1; 

当您创建它(不使用self.创建它),分配sectionIndex

MoreButton *moreButton = [MoreButton buttonWithType:UIButtonTypeCustom]; 
moreButton.sectionIndex = section; 
[moreButton addTarget:self action:@selector(moreButtonSelected:) forControlEvents:UIControlEventTouchUpInside]; 
[view addSubview:moreButton]; 

然后在您的处理程序:

- (void)moreButtonSelected:(MoreButton *)button { 
    self.expandedSection = button.sectionIndex; 
    [self.matchCenter reloadData]; 
} 

而且你的身高为行:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == self.expandedSection || indexPath.row <= 3) { 
    return 65; 
    } 
    return 0; 
} 

您可以使用您的expandedSectionsNSMutableSet,而是如果你想扩展多个在同一时间,使用这些行:

@property (strong) NSMutableSet *expandedSections; 
self.expandedSections = [NSMutableSet set]; 

if ([self.expandedSections containsObject:@(section)]) { 
    [self.expandedSections removeObject:@(section)]; 
} else { 
    [self.expandedSections addObject:@(section)]; 
} 

if ([self.expandedSections containsObject:@(indexPath.section)] || indexPath.row <= 3) 
+0

啊有道理。不过,我有点困惑,“将更多按钮的'indexPath.section'存储到'_expandedSection'中意味着什么。向我展示一个例子?谢谢! – Ghobs 2014-10-10 18:47:22

+0

为我更新了我的答案。 – 2014-10-10 19:17:30

+0

它给我下面的错误:'对于架构x86_64的未定义符号: “_OBJC_CLASS _ $ _ MoreButton”,从引用: objc级,裁判在MatchCenterViewController.o LD:符号(S)没有发现建筑x86_64的 铛:错误:链接器命令失败,退出代码1(使用-v查看调用) ' – Ghobs 2014-10-10 20:34:11

1

我想的问题是,indexPath.row并不总是使用 - 当_hasPressedShowMoreButton是真实的,它无论返回65:

if (_hasPressedShowMoreButton == YES){ 
    return 65; 
} else if (_hasPressedShowMoreButton == NO){ 
    if (indexPath.row > 3){ 
     return 0; 
    } 
    else{ 
     return 65; 
    } 
} 

你需要改变它的东西更多在以下结构中,判断indexPath.row与最外面的如果:

if (indexPath.row > 3) { 
    //some logic here 
} else { 
    //some logic 
} 
+0

请检查我的编辑。 – Ghobs 2014-10-10 19:12:35

相关问题