简体   繁体   English

如何扩展UITableView单元?

[英]How to Unexpand an expanded UITableView Cell?

I'm expanding my cell via: 我通过以下方式扩展我的细胞:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(selectedRow && indexPath.row == selectedRow.row) {
        tableView.scrollEnabled = NO;
        return 480;
    }
    return 250;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    selectedRow = indexPath;
    [tableView beginUpdates];
    [tableView endUpdates];

    CGFloat height = 250.0f;//cell height
    tableView.contentInset = UIEdgeInsetsMake(0, 0, height * indexPath.row, 0);

   [tableView setContentOffset:CGPointMake(0, (indexPath.row * height) )animated:YES];

}

It works fine, whenever I select a cell, it do expands, but I couldn't figure out how to bring it back to its original height. 它工作正常,每当我选择一个单元格时,它确实会扩展,但我无法弄清楚如何将其恢复到原始高度。 Any thoughts? 有什么想法吗?

Try This : 尝试这个 :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
        if(selectedRow.row == indexPath.row)
              selectedRow = nil;
        else
              selectedRow = indexPath;
        [tableView beginUpdates];
        [tableView endUpdates];

        [tableView reloadData];   
}

Try this 尝试这个

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(selectedRow && indexPath.row == selectedRow.row) {
        tableView.scrollEnabled = NO;
        return 480;
    }
    tableView.scrollEnabled = YES;
    return 250;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 250.0f;
    if (selectedRow && indexPath.row == selectedRow.row) {
        NSIndexPath *index = [NSIndexPath indexPathForRow:selectedRow.row inSection:selectedRow.section];
        selectedRow = nil;
        [tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];

        tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        [tableView setContentOffset:CGPointMake(0, 0 )animated:NO];

    } else {
        selectedRow = indexPath;
        [tableView beginUpdates];
        [tableView endUpdates];

        tableView.contentInset = UIEdgeInsetsMake(0, 0, height * indexPath.row, 0);
        [tableView setContentOffset:CGPointMake(0, (indexPath.row * height) )animated:NO];
    }

}

I would suggest you to add a custom property like isExpanded in your tableView datasource object and switch that property based on selection and return height according to this value. 我建议你在tableView数据源对象中添加一个自定义属性,如isExpanded ,并根据选择和返回高度根据此值切换该属性。

For ex: In your heightForRowAtIndexPath method, you could have somthing like this. 例如:在你的heightForRowAtIndexPath方法中,你可能有这样的事情。

if(yourCustomCell.isExpanded == true){
    return 480;
}
else{
    return 250;
}

In didSelectRowAtIndexPath method, just set or unset this property. didSelectRowAtIndexPath方法中,只需设置或取消设置此属性即可。

yourCustomCell.isExpanded = !yourCustomCell.isExpanded
[tableView reloadData]; //Which will call heightForRow and then assign new height based on the property value.

Now if you dont have a custom cell object, then I think you can make use of cell's isSelected property. 现在,如果您没有自定义单元格对象,那么我认为您可以使用单元格的isSelected属性。

I haven't tried these, but I guess this should work. 我没试过这些,但我想这应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM