如何在UITableView的每个部分中有不同的行高?

时间:2014-10-07 17:25:57

标签: ios objective-c uitableview

我目前在每个部分的viewForFooterInSection都有一个按钮,当点击时,将行高5-10从高度0增加到高度65,如下所示:

- (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;
        }
    }

}

2 个答案:

答案 0 :(得分:1)

我猜的问题是并不总是使用indexPath.row - 当_hasPressedShowMoreButton为true时,无论如何返回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
}

答案 1 :(得分: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;
}

如果您想使用这些行一次扩展多个,则可以使用NSMutableSet代替expandedSections

@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)