0

Contacts.app で連絡先を表示するのと同様のビューを実行しています。つまり、さまざまなセクション (電話、メールなど) の下に、必須ではない複数のフィールド (2 番目の職場の電話、2 番目のメールなど) があります。

一部のフィールドが空の場合は表示したくありません。セクションの下のすべてのフィールドが空の場合は、このセクションを表示したくありません。また、電話番号のセルをタップすると、表示されている番号に電話をかけるなど、一部のセルにはアクションがあります。現在、これらのアクションはdidSelectRowAtIndexPath、セルの位置に応じて基本的な if を使用して手動で処理されます。

これをすべて行うためのエレガントなソリューションが見つかりません...セクションごとに一連の辞書(各セル)を試しましたが、物事は急速に混乱しました。また、行の順序が同じになることはないためdidSelectRowAtIndexPath、セルの位置に基づいてメソッド内のすべてのアクションを ifs で簡単に処理することはできません。

ああ、私は内部で Core Data を使用しています。

似たようなことをしなければならなかった人がいて、彼の考えを共有してくれる人はいますか?

ありがとう!

セルを区別するための私の静的 ifs セットアップの例:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (indexPath.section == 0)
    {
        // Custom actions here
    }
    else if (indexPath.section == 1)
    {
        // Other actions here
        [self showMailComposeWithEmail:cell.textLabel.text];
    }
}

スタイルと動作を区別するために indexPath を使用する別の方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            cell.textLabel.text = self.contact.phone;
        }
    }
    else if (indexPath.section == 1)
    {
        if (indexPath.row == 0)
        {
            cell.textLabel.text = self.contact.email;
        }
    }

    return cell;
}
4

1 に答える 1

1

辞書を取り、その辞書にオブジェクトを追加するときは、その中のデータが空白でないことを確認してください。以下のように、キーの辞書にデータを追加します。

Phones - key 0^0 (it means at 0 section 0 row)
WorkPhone - key 0^1(it means at 0 section 1st row)
Emails - key1^0 (1 section 0th row) and so on...

そして、cellForRowAtIndexPathこの値を次のようにフェッチします

[dict valueForKey:[NSString stringWithFormat:@"%d^%d",indexPath.section,indexPath.row]];

それが明確であることを願っています。

于 2013-01-02T04:32:44.367 に答える