3

我想设计一个需要用户输入一些东西的应用程序,比如开始日期、结束日期、一堆其他选项和一些文本评论,我打算使用选择器来选择将模态向上滑动的数据。我需要向上和向下移动视图,以确保当拾取器和键盘上下滑动时填写的元素保持焦点。

我的问题是实施这种“形式”的最佳观点是什么?我在考虑分组表视图,我可以在其中明智地分隔字段。

有没有其他方法可以实现这些东西?根据经验或最佳实践,是否有更好的替代方案或示例代码或应用程序可供我探索?

开发。

4

1 回答 1

7

最类似于 iPhone 的表单界面将是分组表格视图。这是大多数用户在使用其他使用分组表视图来添加和编辑结构化数据的应用程序之后所期望的。

enum一个好的做法是为部分和部分内的行创建一个(枚举),例如:

typedef enum {
    kFormSectionFirstSection = 0,
    kFormSectionSecondSection,
    kFormSectionThirdSection,
    kFormSections
} FormSection;

typedef enum {
    kFormFirstSectionFirstRow = 0,
    kFormFirstSectionSecondRow,
    kFormFirstSectionRows
} FormFirstSectionRow;

...

在此示例中,您可以使用此枚举按名称而不是编号来引用节。

(在实践中,您可能不会使用kFormSectionFirstSection描述性名称,而是使用类似kFormSectionNameFieldSectionkFormSectionAddressFieldSection等之类的名称,但这应该有望说明enum.)的结构。

你会怎么用这个?

下面是几个表视图委托方法的示例,演示了它的用途:

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

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case kFormSectionFirstSection:
            return kFormFirstSectionRows;

        case kFormSectionSectionSection:
            return kFormSecondSectionRows;

        ...

        default:
            break;
    }
    return -1;
}

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

    // cell setup or dequeue...

    switch (indexPath.section) {
        case kFormSectionThirdSection: { 
            switch (indexPath.row) {
                case kFormThirdSectionFourthRow: {

                    // do something special here with configuring 
                    // the cell in the third section and fourth row...

                    break;
                }

                default:
                    break;
            }
        }

        default:
            break;
    }

    return cell;
}

这应该很快显示出枚举的实用性和威力。

代码中的名称比数字更容易阅读。当你处理委托方法时,如果你有一个好的描述性的部分或行名称,你可以更容易地阅读表格视图和单元格的管理逻辑。

如果要更改部分或行的顺序,您所要做的就是重新排列enum构造中枚举标签的顺序。您无需进入所有委托方法并更改幻数,一旦您拥有多个部分和行,这很快就会变得棘手且容易出错。

于 2010-07-21T18:47:58.637 回答