在分组的表视图中混合静态和动态部分 [英] Mixing static and dynamic sections in a grouped table view

查看:153
本文介绍了在分组的表视图中混合静态和动态部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个分组的 UITableView 类似于设置应用中的Twitter帐户:

I need a grouped UITableView similar to the one for Twitter accounts in Settings app:

那就是一种形式或菜单,一些部分具有预先知道的一组静态单元格,其他一些部分必须是动态的,并允许插入额外的行,这与添加帐户相同。我在 .xib 文件中管理 UITableView 。对于静态单元格,我已经在视图控制器的 cellForRowAtIndexPath:方法中分离了可以加载的 .xib 文件。

That is, a sort of form or menu where some of the sections have a beforehand known set of static cells, and some other sections have to be dynamic and allow inserting additional rows the same way the "Add Account" does here. I'm managing the UITableView in a .xib file. For the static cells, I have separated .xib files that I can load within the cellForRowAtIndexPath: method in the view controller.

我应该如何处理这种表?我没有找到任何示例代码。

How should I handle this kind of table? I don´t find any example code.

如何将 cellForRowAtIndexPath:方法看起来像?我可以为静态单元格保留 strong 属性吗?在表格视图所在的相同的 .xib 文件中,直接设计每个静态单元格是否更好? (虽然这不允许重用我的自定义单元格设计...)

How the cellForRowAtIndexPath: method should look like? May I need to keep strong properties for the static cells? Would it be better to design each static cell directly within the same .xib file where the table view is, and to set outlets for them? (Though this does not allow to reuse my custom cells design...)

我需要一些准则来实现这一点,正确管理单元格和内存。感谢提前

I need some guidelines for achieving this and correctly managing cells and memory. Thanks in advance

推荐答案

如果您只是在cellForRowAtIndexPath中添加任何内容返回单元格,动态原型单元格可以像静态原型单元一样运行,因此您可以使用动态原型,同时拥有静态单元格和动态单元格(其中行数和内容可变)。

Dynamic prototype cells can behave like static ones if you just return the cell without adding any content in cellForRowAtIndexPath, so you can have both "static like" cells and dynamic ones (where the number of rows and the content are variable) by using dynamic prototypes.

在下面的示例中,我在IB中启动了一个表视图控制器(具有分组的表视图),并将动态原型单元格的数量更改为3.我将第一个单元格的大小调整为80,并添加了一个UIImageView和两个标签。中间单元格是基本样式单元格,最后一个是具有单个居中标签的另一个自定义单元格。我给他们每个自己的标识符。这是IB的样子:

In the example below, I started with a table view controller in IB (with a grouped table view), and changed the number of dynamic prototype cells to 3. I adjusted the size of the first cell to 80, and added a UIImageView and two labels. The middle cell is a Basic style cell, and the last one is another custom cell with a single centered label. I gave them each their own identifier. This is what it looks like in IB:

然后在代码中,我这样做:

Then in code, I did this:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.theData = @[@"One",@"Two",@"Three",@"Four",@"Five"];
    [self.tableView reloadData];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1)
        return self.theData.count;
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0)
        return 80;
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;

    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"TitleCell" forIndexPath:indexPath];

    }else if (indexPath.section == 1) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"DataCell" forIndexPath:indexPath];
        cell.textLabel.text = self.theData[indexPath.row];

    }else if (indexPath.section == 2) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"ButtonCell" forIndexPath:indexPath];
    }

    return cell;
}

正如你所看到的,对于静态单元格,我只是返回该单元格具有正确的标识符,我正好在IB中设置了它。运行时的结果将看起来像您的三张图片。

As you can see, for the "static like" cells, I just return the cell with the correct identifier, and I get exactly what I set up in IB. The result at runtime will look like your posted image with three sections.

这篇关于在分组的表视图中混合静态和动态部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆