【问题标题】:tableView implementation like on appStore [closed]像appStore上的tableView实现[关闭]
【发布时间】:2014-02-04 08:47:53
【问题描述】:

之前

之后

基本上我的观点是我有 customTableViewCell,它在最右边有按钮(与 image1“显示版本历史记录”相同)。当我单击时,我想添加一个动态数量的 subviewCell,它位于 customTableViewCells 下。

我的问题是,这是如何实施的?有什么图书馆可以参考吗?

【问题讨论】:

  • 哪种方法最好?你怎么解决的?

标签: ios uitableview ios7


【解决方案1】:
【解决方案2】:
【解决方案3】:

这很容易。

  • 您想跟踪该部分是否已展开。为此使用实例变量(或属性)
  • 如果未展开,则在 tableView:numberOfRowsInSection: 中为可展开部分返回 1
  • 如果它被扩展,则返回 1 + t:numberOfRowsInSection: 中扩展单元的数量
  • tableView:didSelectRowAtIndexPath: 中,您可以在点击第一个单元格并插入或删除展开的行时切换展开状态。

例如:

@interface MBMasterViewController () {
    BOOL firstSectionExpanded;
}
@end

@implementation MBMasterViewController

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = 1;
    if (section == 0 && firstSectionExpanded) { count = 3; }
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.textLabel.text = [NSString stringWithFormat:@"Section %ld", (long)indexPath.section];
    }
    else {
        cell.textLabel.text = [NSString stringWithFormat:@"Sub Cell %ld", (long)indexPath.row];
    }
    return cell;
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row != 0) {
        // don't select sub cells
        return nil;
    }
    return indexPath;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *cells = @[ [NSIndexPath indexPathForRow:1 inSection:indexPath.section],
                        [NSIndexPath indexPathForRow:2 inSection:indexPath.section]];

    NSArray *cellsToDelete;
    NSArray *cellsToInsert;
    if (indexPath.section == 0) {
        if (firstSectionExpanded) {
            cellsToDelete = cells;
        }
        else {
            cellsToInsert = cells;
        }
        firstSectionExpanded = !firstSectionExpanded;
    }

    if (cellsToDelete) {
        [tableView deleteRowsAtIndexPaths:cellsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    if (cellsToInsert) {
        [tableView insertRowsAtIndexPaths:cellsToInsert withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    相关资源
    最近更新 更多