![](/img/trans.png)
[英]A grouped cell is showing up in my plain tableView and I don't know why
[英]Don't display empty grouped cell tableview
我正在使用與Contacts.app中的“ 查看聯系人”相似的視圖,這意味着我在不同部分(電話,電子郵件等)下有多個字段(第二部工作電話,第二部電子郵件等)不是必需的。
當某些字段為空時,我不想顯示它們;當某個字段下的所有字段都為空時,我不想顯示此部分。 另外,某些單元格會對它們起作用,例如,在電話號碼單元格上點擊時,它將呼叫顯示的號碼。 當前,這些操作是在didSelectRowAtIndexPath
通過基本ifs手動處理的,具體取決於單元格位置。
我找不到合適的解決方案來完成所有這些工作……我為每個部分嘗試了一系列詞典(每個單元格),但是事情很快就變得混亂了。 另外,由於行的順序永遠都不相同,因此,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;
}
拿一個字典,當您向該字典中添加對象時,請確保其中的數據不是空白。 將其數據添加到字典的鍵中,如下所示:
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]];
希望那很清楚。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.