简体   繁体   中英

Don't display empty grouped cell tableview

I'm doing a similar view to View Contact in Contacts.app, meaning I have multiple fields that are not required (second work phone, second email, etc.) under different sections (Phones, Emails, etc.).

When some fields are empty, I don't want to display them, when all the fields are empty under a section, I don't want to display this section. Also, some of the cells have action on them, like when tapping on a phone number cell, it calls the number displayed. Currently, these actions are manually handled in didSelectRowAtIndexPath with basic ifs depending of the cell position.

I can't find an elegant solution to do all this... I tried an array of dictionaries (each cell) for each section but things rapidly went messy. Also, since the order of the rows is never the same, I can't easily with ifs handle all the actions in the didSelectRowAtIndexPath method based on the cell position.

Oh and I'm using Core Data under the hood.

Anybody had to do something similar and is willing to share his thoughts?

Thanks!

Exemple of my static ifs setup right now to differentiate cells:

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

And another method using indexPath to differentiate styles and behaviors:

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

Take a dictionary and when you add object to that dictionary make sure that the data in it is not blank. Add it data in dictionary for keys as below:

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...

And at cellForRowAtIndexPath fetch this value as

[dict valueForKey:[NSString stringWithFormat:@"%d^%d",indexPath.section,indexPath.row]];

Hope thats clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM