フォームの最も iPhone に似たインターフェイスは、グループ化されたテーブル ビューになります。これは、グループ化されたテーブル ビューを使用して構造化データを追加および編集する他のアプリを使用した後、ほとんどのユーザーが期待することです。
enum
セクションおよびセクション内の行に対して (列挙)を作成することをお勧めします。たとえば、次のようになります。
typedef enum {
kFormSectionFirstSection = 0,
kFormSectionSecondSection,
kFormSectionThirdSection,
kFormSections
} FormSection;
typedef enum {
kFormFirstSectionFirstRow = 0,
kFormFirstSectionSecondRow,
kFormFirstSectionRows
} FormFirstSectionRow;
...
この例では、この列挙を使用して、番号ではなく名前でセクションを参照できます。
(実際には、おそらくkFormSectionFirstSection
説明的な名前として使用するのではなく、kFormSectionNameFieldSection
またはkFormSectionAddressFieldSection
などのようなものを使用しますが、これは の構造を示しているはず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
。すべてのデリゲート メソッドにアクセスしてマジック ナンバーを変更する必要はありません。これは、セクションと行が 2 つ以上になると、すぐにトリッキーでエラーが発生しやすいダンスになります。