我正在对Contacts.app中的查看联系人进行类似的查看,这意味着我在不同的部分下有多个不需要的字段(第二部工作电话,第二部电子邮件等) ,电子邮件等)。
当某些字段为空时,我不想显示它们,当某个部分下的所有字段都为空时,我不想显示此部分。此外,一些单元格对它们有动作,就像点击电话号码单元格一样,它会调用显示的数字。目前,这些操作在didSelectRowAtIndexPath
中手动处理,基本ifs取决于单元格位置。
我无法找到一个优雅的解决方案来完成所有这些...我为每个部分尝试了一系列字典(每个单元格),但事情很快变得混乱。此外,由于行的顺序永远不会相同,因此我无法使用ifs根据单元格位置处理didSelectRowAtIndexPath
方法中的所有操作。
哦,我正在使用核心数据。
任何人都必须做类似的事情并且愿意分享他的想法吗?
谢谢!
立即执行我的静态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;
}
答案 0 :(得分: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]];
希望明白。