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

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

问题描述

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

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

也就是说,一种表单或菜单,其中某些部分具有一组预先已知的静态单元格,而其他一些部分必须是动态的,并允许像添加帐户"在此处所做的那样插入额外的行.我在 .xib 文件中管理 UITableView.对于静态单元格,我已经分离了 .xib 文件,我可以在视图控制器的 cellForRowAtIndexPath: 方法中加载这些文件.

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中的一个table view controller(带有分组的table view)开始,将动态原型单元格的数量改为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天全站免登陆