1

次のような numberOfSections... メソッドがあります。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    BOOL showContacts = self.selectedReminderMessageService != RMReminderMessageServiceTwitter ? YES : NO;

    if (self.isEditing) {

        if (showContacts) {

            return 5;

        } else {

            return 4;
        }

    } else {

        if (showContacts) {

            return 4;

        } else {

            return 3;
        }
    }
}

cellForRowAtIndexPath... メソッドはどのように作成すればよいですか? 次のように、考えられるすべての構成をリストする必要がありますか?

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

    BOOL showContacts = self.selectedReminderMessageService != RMReminderMessageServiceTwitter ? YES : NO;

    NSInteger section = [indexPath section];

    if (self.isEditing) {
         if (showContacts) {
              if (section == 0) {
                  // repeat to section 4 with else if's
              }
         } else {
              if (section == 0) {
                  // repeat to section 3 else if's
              }
         }
     } else {
         if (showContacts) {
              if (section == 0) {
                  // repeat to section 3 else if's
              }
         } else {
              if (section == 0) {
                  // repeat to section 2 else if's
              }
         }
     }
}

これをより効率的な方法で行うことはできますか?

4

2 に答える 2

2

私は同様の問題を抱えており、indexPath(またはセクション)を指定してそれがどのセクションであるかを返す列挙子とメソッドを作成することになりました。

そうすれば、特定のインデックスで扱っているセルのタイプを見つける必要があるときはいつでも (たとえば、セルの作成と選択)、そのメソッドにそれがどのタイプであるかを尋ねるだけです。

例:

typedef enum {
    SectionNone = 0,
    SectionContacts,
    SectionOptions,
} Section; // do a more appropriate name

- (Section)sectionForSection:(NSInteger)section {
    // evaluate your state and return correct section
}

だからあなたのcellForRowで...あなたは行くことができます

Section sect = [self sectionForSection:indexPath.section];
switch (sect) {
    case SectionContacts: {
        // work with contact cell
        break;
    }
    case SectionOptions: {
        // work with options cell
        break;
    }
    // etc
}
于 2012-08-30T20:13:34.113 に答える
0

したがって、isEditing が true の場合に表示される追加のセクションと、showContacts が true の場合に表示される追加のセクションがあり、それぞれの条件が false の場合、これらのセクションは表示されません。私はその権利を持っていますか?if/else次に、 tableView:cellForRowAtIndexPath: メソッドの sを少なくする方法についての質問ですか?

まず、常に同じ数のセクション (この場合は 5) を from から返しnumberOfSectionsInTableViewます。次に、tableView:numberOfRowsInSection:条件をチェックして、false の場合は 0 を返し、true の場合は適切な行数を返します。

ここで、セクション 0 は常に「連絡先」セクションであり、セクション 4 は常に「行の追加」セクション (または任意の順序で配置) です。最後に、tableView:cellForRowAtIndexPath:メソッドでは、セルの正しい「タイプ」を作成するために、どのセクションにいるのかを確認する必要があります。そのセクションに行がない場合、そのコードは決して実行されません。

if (indexPath.section == 0) {
  //contacts cell 
} else if (indexPath.section == 1) {
  //cell for whatever section 1 is
} else if (indexPath.section == 2) {
  //etc.
} //etc.

必要に応じて、セクションに名前を付けるための Ismael のアイデアとこれを組み合わせることができますが、コメントでセクションを示す以上のことを行う必要があるとは思いませんでした。

于 2012-08-30T20:30:46.370 に答える