我正在执行与 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;
}