2013-01-01 66 views
0

我正在做一个类似的视图查看联系人在Contacts.app,这意味着我有不同的领域不需要(第二个工作电话,第二封电子邮件等)不同部分(电话,电子邮件等)。不显示空的分组单元tableview

当某些字段是空的,我不想显示它们,当所有的字段下部分空,我不想显示该节。此外,有些单元对它们有操作,就像在电话号码单元上敲击时一样,它会调用显示的号码。目前,这些操作在didSelectRowAtIndexPath中手动处理,具有基本ifs,具体取决于单元位置。

我不能找到一个很好的解决方案做了这一切......我尝试字典数组(每个单元),每个部分,但事情很快就乱了。另外,由于行的顺序绝不相同,我不能轻易地用if来处理基于单元位置的didSelectRowAtIndexPath方法中的所有操作。

哦,我在引擎盖下使用核心数据。

任何人都必须做同样的事情,并愿意分享他的想法吗?

谢谢!使用indexPath区分风格和行为

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

而另一种方法:

现在我的静态IFS设置的为例分化细胞

- (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; 
} 
+0

你可以展示在其中添加数据的代码 – Dhara

+0

你怎么从那里我添加数据意味着什么? – allaire

+0

您是否将数据添加到数组或字典? – Dhara

回答

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

希望这就是明确的。

+0

感谢您的回答。几个问题:你会怎么做'numberOfRowsInSection:'和'numberOfSectionsInTableView:'方法。另外,'cellForRowAtIndexPath:'和'didSelectRowAtIndexPath:'中有ifs,你将如何区分单元格,因为我不能再依赖indexPath了(现在如果手机是空的并且不显示等,那么电子邮件可以是第一个等等。 )。查看我编辑的答案,了解我当前的一些代码。谢谢! – allaire

+0

如果您可以根据编辑的答案添加更多代码,我会立即接受该答案,我非常感谢您的时间,谢谢。 – allaire

+0

当然,我可以帮助你。但是,你能告诉我你在哪里以及如何填充联系数组中的数据。 – Dhara