You are previewing Pro iOS Table Views and Collection Views: Using Swift 2.
O'Reilly logo
Pro iOS Table Views and Collection Views: Using Swift 2

Book Description

Pro iOS Table and Collection Views with Swift takes a task-oriented focus to assist you when implementing customized table views or collection views. It’s a great reference and customization cookbook at the same time, useful for beginners as well as intermediate developers. You'll learn how to create table and collection views for a variety of screen types, including the new Watch, using the Swift 2 programming language. But this book doesn't leave the Objective-C programmer behind -- you'll also find code samples for Objective-C.

If you’re an iOS app developer, chances are you’ll be using table views and now collection views in your development projects. Table views are the bread and butter of iOS apps. With them, you can create everything from the simplest of lists to fully tricked-out user interfaces, and the possibilities are even greater now with Watch glances and a larger variety of screen sizes.

Table views and collection views are some of the most complex components found in UIKit. While using them for boring standard user interfaces is quite simple, customizing them can become really challenging.

  • Covers the entire Table View and Collection View APIs in depth
  • Covers customization and performance topics in depth
  • Task-oriented reference with multiple levels of detail
  • Table of Contents

    1. Cover
    2. Title
    3. Copyright
    4. Dedication
    5. Contents at a Glance
    6. Contents
    7. About the Author
    8. About the Technical Reviewers
    9. Acknowledgments
    10. Introduction
    11. Chapter 1: Table Views Quick Start
      1. What Are Table Views?
      2. The Anatomy of a Table View
      3. Creating a Simple Table View App
      4. Creating the Application Skeleton
      5. Generating Some Data
      6. Creating the Table View
      7. Conforming to the Table View Protocols
      8. Wiring Up the Data Source and Delegate
      9. Displaying the Data
        1. numberOfSectionsInTableView( :)
        2. tableView:numberOfRowsInSection( :)
        3. Creating Cells
        4. Creating a Prototype Cell
        5. Configuring the Cell
        6. Running the App
      10. Adding Some Interactivity
        1. tableView:didSelectRowAtIndexPath:
      11. Understanding How the App’s Objects Fit Together
      12. Summary
    12. Chapter 2: How the Table View Fits Together
      1. Understanding Table Views
      2. Working with the UITableView Family
        1. The UITableView Class Hierarchy
      3. Choosing the Type of Table View
        1. The Plain Table
        2. The Indexed Table
        3. The Sectioned Table
        4. The Grouped Table
      4. Setting TableView Dimensions
      5. Controlling the Background of a UITableView
      6. What UITableView Inherits from UIScrollView
      7. Creating UITableViews
        1. Creating a UITableView in Interface Builder
        2. Creating a UITableView Programmatically
        3. Creating a UITableView with UITableViewController
      8. Summary
    13. Chapter 3: Collection Views Quick Start
      1. What Are Collection Views?
      2. The Anatomy of a Collection View
        1. The Collection View Itself
        2. Collection View Cells
        3. The Supporting Objects
      3. Creating a Simple Collection View App
      4. Creating the Application Skeleton
      5. Creating Some Data
        1. Adding the Card Images
        2. Building the Model
        3. Setting Up the Collection View in the Storyboard
        4. Setting Up the delegate and dataSource Funtions
        5. The App in Action
      6. Summary
    14. Chapter 4: How The Collection View Fits Together
      1. What Are Collection Views?
      2. The Architecture of a Collection View
      3. The Anatomy of a Collection View
        1. The Collection View Itself
        2. Collection View Items
        3. Collection View Layouts
        4. The Supporting Objects in Detail
      4. Creating Collection Views
        1. Creating a UICollectionView with Interface Builder
        2. Creating a UICollectionView in Code
      5. Summary
    15. Chapter 5: Feeding Data to Your Views
      1. UITableView, UICollectionView, and Delegation
        1. Understanding Delegation
        2. Setting Delegates
        3. Wiring Up an Object with a Delegate
        4. Defining Protocols
        5. Using UITableView’s Delegate Methods
        6. Using UITableViewDelegate Methods
        7. Using UICollectionViewDelegate Methods
      2. Datasources
      3. The UITableView dataSource
        1. How the Key Information Is Obtained by the Table
        2. Cell-, Section-, and Row-Related UITableViewDataSource Methods
        3. Title- and Index-Related UITableViewDataSource Methods
        4. Insertion-, Removal-, and Reordering-Related UITableViewDataSource Methods
      4. The UICollectionView dataSource
        1. How the Key Information Is Obtained by the Collection View
        2. Which configuration method should I use?
        3. Cell-, Section-, and Item-Related UICollectionViewDataSource Methods
      5. The Thing to Bear in Mind About dataSource Methods
      6. Implementing the dataSource and delegate Protocols
      7. All About indexPaths
      8. The Model-View-Controller Design Pattern
        1. Why Use the Model-View-Controller Pattern?
        2. MVC and iOS
        3. MVC, tableViews, and collectionViews
      9. Improving the App Structure
        1. How to Split Out Datasources and Delegates
      10. Summary
    16. Chapter 6: How the Table Cell Fits Together
      1. Understanding the Anatomy of a UITableViewCell
        1. Basic Structure of the Cell
        2. The Cell’s Background Views
        3. Content and Accessory Views
      2. Designing Prototype Cells
        1. Creating Prototype Cells in XIB Files
        2. Creating Prototype Cells in Storyboards
        3. Creating Prototype Cells in Code
      3. Working with Standard Cell Types
        1. Using UITableViewCellStyleDefault
        2. Using UITableViewCellStyleValue1
        3. Using UITableViewCellStyleValue2
        4. Using UITableViewCellStyleSubtitle
      4. Configuring the Default Cell’s Content
        1. textLabel
        2. detailTextLabel
        3. imageView
        4. contentView
        5. Formatting Text in Default Cell Types
      5. Working with Accessory Views
        1. Using UITableViewCellAccessoryDisclosureIndicator
        2. Using UITableViewCellAccessoryDetailDisclosureIndicator
        3. Using UITableViewCellAccessoryCheckmark
        4. Using UITableViewCellAccessoryNone
        5. Setting the Accessory View Type
        6. Using an Accessory View to Show Cell Selection State
        7. Creating Custom Accessory Views
      6. Creating and Reusing Cells
        1. Memory Limitations
        2. Speed and Smoothness
        3. Just-in-Time Creation and Recycling
        4. The Table View’s “Conveyor Belt”
        5. Side Effects of Cell Reuse and Caching
      7. Summary
    17. Chapter 7: Improving the Look of Cells
      1. Customizing Cells
        1. Which Function Should I Use?
      2. Adding Subviews to the Cell’s contentView
        1. Creating the Elements in the Cell
      3. Creating Custom Cells Visually As Prototypes In A Storyboard
        1. Creating Prototype Dynamic Cells
      4. Creating Custom Cells Visually Using Interface Builder
        1. The Stages of Creating Cells Visually
        2. Creating a New XIB File
        3. Creating the Cell’s Content
        4. Registering the Cell
        5. Controlling Cell Sizes
      5. Handling Cell Resizing in Tables
      6. Summary
    18. Chapter 8: Creating Custom Cells with Subclasses
      1. Why Create a Custom Cell Subclass?
        1. The Process of Creating Custom Cells
      2. Custom Cells with XIBs
        1. Designing Your Cell
        2. Creating the Class for the Custom Cell
        3. Building the Cell in Interface Builder
        4. Creating Instances of the Custom Cells
      3. Handling Selection in Custom Cells
      4. Custom Cells in Code
        1. The Process of Custom Cells in Code
        2. Creating Custom Subclasses
        3. Overriding the layoutSubviews Function
        4. Overriding the prepareForReuse Function
      5. Improving the App’s Architecture with MVVM
        1. The Model-View-View Model Approach
        2. Advantages and Disadvantages of MVVM
        3. Implementing the MVVM Approach
        4. Converting the Project to an MVVM Approach
      6. Summary
    19. Chapter 9: Improving Interaction
      1. Embedding Custom Controls into Cells
        1. A Simple Approach – Adding a Button Directly To The Cell
        2. Creating the Buttons
        3. Adding the Buttons to Cells
        4. Reacting to Individual Controls
        5. A More Robust Subclass-based Approach
      2. Adding Gestures to Cells
      3. Adding Pull-to-Refresh to Table Views
        1. Implementing Pull-to-Refresh with UITableViewController
        2. Implementing the pullToRefresh Function
        3. Adding a UIRefreshControl to a Table View
      4. Searching in Tables and Collection Views
        1. Adding a Search Bar to the Table
        2. How Search Works
        3. Implementing Search
      5. Happy, Healthy Tables
        1. Background, Background, Background
        2. Are the Cells Cached?
        3. Do Your Table Cells Have Varying Heights?
        4. Cutting the Cost of Compositing
      6. Summary
    20. Chapter 10: Using Tables for Navigation
      1. The Navigation Controller Interface Pattern
      2. Introducing the UINavigationController
      3. Creating a Navigation Controller App
        1. Creating the Name Class
        2. Creating Some Dummy Data
        3. Connecting Up the Table View
        4. Building the Detail View
        5. Passing Data into the Detail View
        6. Implementing the Navigation Controller
        7. How the Navigation Controller Is Wired Up
        8. Linking the Navigation Controller and Detail Views Together
      4. Building Navigation Structure with Segues
        1. Embedding the Table View in a Navigation Controller
        2. Updating the App Delegate
        3. Linking the Detail View to the Table View
      5. Summary
    21. Chapter 11: Indexing, Grouping, and Sorting Tables
      1. Using Indexed Tables
      2. Using Sectioned and Grouped Tables
      3. Creating a Simple Indexed Table
        1. Setting Up the Basic Table
        2. Creating the Source Data
        3. Feeding the Table with Data
      4. Building Practical Sectioned Tables
      5. Creating the Data for a Table with Sections and Indexes
        1. Arrays of Arrays
        2. UILocalizedIndexedCollation
      6. Creating the All-Singing, All-Dancing Table
        1. Creating the App from a Template
        2. Creating Some Data in a plist File
        3. Sorting Out the User Interface
        4. Extending the ViewController Class
      7. Creating Table and Section Header and Footer Views
      8. Table Headers and Footers
        1. Tidying the Bottom of Tables
      9. Moving the Table Programmatically
        1. scrollToRowAtIndexPath:atScrollPosition:animated:
        2. scrollToNearestSelectedRowAtScrollPosition:animated:
        3. selectRowAtIndexPath:animated:scrollPosition:
      10. Finding the Current Scroll Position in the Table
      11. Summary
    22. Chapter 12: Selecting and Editing Content
      1. A Recap of the Model-View-Controller Pattern
      2. Why the Model-View-Controller Pattern Is Important
      3. Cell Selection in TableViews
        1. Cell Selection Types
        2. Visualizing Selection
        3. Optimizing Selection Performance
      4. Selection Dos and Don’ts
      5. Responding to Selections with More Detail
      6. Design Patterns and UITableViews
        1. Read
        2. Create
        3. Update
        4. Delete
      7. Custom Row Actions
      8. Inserting and Deleting Rows
        1. Putting the Table into Editing Mode
        2. Controlling Whether Rows Can Be Edited
        3. Controlling Each Row’s Editing Style
        4. Dealing with Row Deletions
        5. Dealing with Row Insertions
        6. Rearranging Tables
        7. Moving Rows Around
        8. Enabling Batch Insertion and Deletion
        9. Batch Insertion and Deletion of Sections
      9. Selection in UICollectionViews
        1. Cut, Copy, and Paste with Collection Views
        2. Implementing Custom Menus in a Collection View
      10. Rearranging UICollectionViews
        1. Prerequisites
        2. Implementing Drag-and-Drop with UICollectionViewController
        3. Implementing Drag-and-Drop with UIViewController
      11. Summary
    23. Chapter 13: Static Tables
      1. How to Build Static Tables
        1. Adding Static Cells to the Table View
        2. Fixing Scrolling
        3. Adding Controls to the Static Cells
      2. Using Static Tables Inside Container Views
        1. Prerequisites
        2. Adding a UIViewController Scene
        3. Adding a Container View to the UIViewController
        4. Embedding the Static Table View into the View Controller
      3. Other Uses for Static Tables
      4. Summary
    24. Chapter 14: Tables in WatchKit
      1. About WatchKit
      2. The Anatomy of a WatchKit App
      3. What Are WatchKit Tables?
      4. Creating a Basic Table
        1. Creating the Project
        2. Adding the WatchKit Target
        3. Building the Table Interface
        4. Creating the Table
      5. Navigation with WatchKit Tables
        1. Adding a New Interface Controller
        2. Adding a New Screen to the Storyboard
        3. Implementing the Navigation
        4. Adding Navigation in Code
      6. Summary
    25. Chapter 15: Collection View Flow Layouts
      1. About Flow Layouts
      2. The Characteristics of a Flow Layout
      3. UICollectionViewFlowLayout
      4. Creating and Configuring Flow Layouts
        1. Instantiating a Flow Layout
      5. Customizing Flow Layouts
        1. Customizing with Attributes
        2. Customizing with UICollectionViewDelegateFlowLayout
      6. Subclassing UICollectionViewFlowLayout
        1. Controlling Item Layout Attributes
        2. Adding Additional Custom Layout Attributes to Items
        3. Adding New Supplementary Views
        4. Controlling Insertion and Deletion Animations
      7. Summary
    26. Chapter 16: Collection View Custom Layouts
      1. About Custom Layouts
        1. When to Create a Custom Collection View Layout
        2. Creating a Custom Layout Subclass
        3. Deciding When to Calculate Attributes
        4. What the Custom Layout Does
        5. What Are Layout Attributes?
        6. The Four Key Functions to Implement
        7. prepareLayout
        8. collectionViewContentSize
        9. layoutAttributesForElementsInRect
        10. layoutAttributesForItemAtIndexPath
      2. Supplementary and Decoration View Attributes
        1. Checking if Supplementary or Decoration Views Are Required
        2. Calculating Supplementary and Decoration View Attributes
      3. This Chapter’s Project
        1. SwiftClock: The “Static” Example
        2. Getting Started
      4. The Initial Project
      5. Updating the Project
        1. Adding the Custom Layout Class
        2. Implementing the Layout Functions
        3. Implementing the Custom Layout Functions
        4. Next Steps
        5. Displaying the Numerals and Hands
      6. Summary
    27. Chapter 17: Animated and Interactive Collection Views
      1. Controlling Collection Views with Gestures
        1. Connecting Gestures with Layouts
      2. Collection Views and Animations
        1. Creating the Custom Layout
        2. Wiring Up the Collection View
        3. Controlling Insertion and Removal Animations
      3. Summary
    28. Index