ManagingContacts/ManagingContacts/DetailViewController.swift

/*
    Copyright (C) 2017 Apple Inc. All Rights Reserved.
    See LICENSE.txt for this sample’s licensing information
    
    Abstract:
    A BaseViewController subclass that displays group or contact 
                information in a plain style table view.
                Destination view controller for the fetchAllGroups, 
                fetchGroupsPerContainer, fetchContactsPerContainer, and 
                fetchContactsPerGroup segues.
*/
 
import UIKit
import Contacts
 
class DetailViewController: BaseViewController {
    // MARK: - UITableViewDelegate
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        // Fetch the menu section object.
        let model = data[indexPath.section]
        
        /* Display groups if category is set to Groups and contacts if it is set 
           to Contacts.
        */
        if  let category = model.category, category == MGCAppConfiguration.Content.groups {
            
            let group = model.content[indexPath.row] as! CNGroup
            cell.textLabel!.text = group.name
        }
        else if let category = model.category, category == MGCAppConfiguration.Content.contacts {
            
            let contact = model.content[indexPath.row] as! CNContact
            cell.textLabel!.text = (contact.isPerson) ? contact.formattedName : contact.organizationName
        }
        else if let category = model.category, (category == MGCAppConfiguration.Content.NoItems.contacts.rawValue || category == MGCAppConfiguration.Content.NoItems.groups.rawValue) {
            cell.textLabel!.text = model.content[indexPath.row] as? String
        }
    }
}