0

我正在执行与 Contacts.app 中的查看联系人类似的视图,这意味着我在不同部分(电话、电子邮件等)下有多个不需要的字段(第二个工作电话、第二个电子邮件等)。

当某些字段为空时,我不想显示它们,当某个部分下的所有字段都为空时,我不想显示该部分。此外,某些单元格对它们有作用,例如在点击电话号码单元格时,它会呼叫显示的号码。目前,这些操作是didSelectRowAtIndexPath根据单元格位置使用基本 if 手动处理的。

我找不到一个优雅的解决方案来完成这一切......我为每个部分尝试了一系列字典(每个单元格),但事情很快就变得一团糟。此外,由于行的顺序永远不会相同,我不能轻松地使用 ifs 处理didSelectRowAtIndexPath基于单元格位置的方法中的所有操作。

哦,我在后台使用 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 回答