ManagingContacts/ManagingContacts/GroupedViewController.swift

/*
    Copyright (C) 2017 Apple Inc. All Rights Reserved.
    See LICENSE.txt for this sample’s licensing information
    
    Abstract:
    A BaseViewController subclass that displays contact or container
                information in a grouped style table view.
                It organizes contacts by type, which is either Person or Organization.
                Containers are organized by Local, Exchange, and CarDAV types.
                Destination view controller for the fetchAllContacts, 
                fetchAllContainers, and fetchDefaultContainer segues.
*/
 
import UIKit
import Contacts
 
class GroupedViewController: BaseViewController {
    // MARK: - UITableViewDataSource
    
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        // Return the header title for this section.
        return data[section].category
    }
 
    
    // MARK: - UITableViewDelegate
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let model = data[indexPath.section]
        
        // Display containers if tab is set to Containers.
        if model.tab == MGCAppConfiguration.Content.containers {
            
            let container = model.content[indexPath.row] as! CNContainer
            cell.textLabel!.text = (container.type == .local) ? MGCAppConfiguration.MainStoryboard.Cells.local : container.name
        }
        // Display contacts if tab is set to Contacts.
        else if model.tab == MGCAppConfiguration.Content.contacts {
            
            let contact = model.content[indexPath.row] as! CNContact
            /* Display the formatted name if the contact is of type Person and
               the organization name if the contact is type of Organization. 
            */
            cell.textLabel!.text = (contact.isPerson) ? contact.formattedName : contact.organizationName
        }
    }
}