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;
}