• Stars
    star
    314
  • Rank 133,353 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Expanding table view cells using UIStackView in iOS 9

ExpandingStackCells

Example code for creating expanding table view cells using stack views, introduced in iOS 9.

What's a stack view?

UIStackView is a view introduced in iOS 9. Its main goal is to make it simpler to organize in our layout elements in either horizontal, or vertical stacks. It also promises to minimize the troubles of Auto Layout (which probably has a lot to do with its popularity ๐Ÿ˜›).

The goal

As the name suggests, the goal is to create expanding table view cells. Prior to iOS 9, there were already multiple ways to achieve this:

This attempt focuses on achieving similar results, but with the use of UIStackView.

Getting started

The benefits of using a stack view become obvious when designing our UI: our table view cell contains one element: a horizontal stack view containing 2 views.

The following layout constraints still had to be defined:

  • Pin the leading/trailing/top/bottom edges of the stack view to the content view
  • Define vertical constraints within the contained view (see the attached screenshot), so the stack view can infer its size properly.

Connecting the dots

In our UITableViewCell subclass, we define outlets for our labels, but more importantly, an outlet for the stack view itself. If you've been following my earlier attempts at expanding cells, you'll notice that this is a slightly different approach: previously, we needed outlets for the height constraints of the contained view. Now, these will be managed by the stack view. ๐Ÿ‘

Updating the contents

A notable feature of UIStackView, is that it can automatically update its layout whenever we tamper with its contents. It maintains the array of contained views in a property called arrangedSubviews. You can dynamically add, or remove views, and the stack view will take care of them out properly.

In our example, we don't want to remove any subviews, only hide them. Luckily, this can be achieved with minimal code.

Step 1:

In our table view cell:

override func setSelected(selected: Bool, animated: Bool) {
   super.setSelected(selected, animated: animated)

   UIView.animateWithDuration(0.5) {
       stackView.arrangedSubviews.last?.hidden = !selected
   }
}

You don't actually need to wrap this into an animation block, but in my opinion, this leads to a prettier result.

Step 2:

You'll need to implement a delegate method to inform the table view of the size change:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
   
   tableView.beginUpdates()
   tableView.endUpdates()
}

Step 3:

There is no Step 3. Enjoy your expanding cells! ๐ŸŽ‰

Conclusion:

As you can see, stack views make implementing expanding cells are super easy. If you compare this example to an earier one, you'll notice that we now have much less code to worry about.

UIStackView is a great addition in iOS 9, and whether you just want to ease the transition into Auto Layout, or have a more maintainable, organized layout, you should definitely give it a try.

More Repositories

1

ExpandingTableView

Framework for expanding table view cells.
Swift
70
star
2

ExpandableTableView

Example code for creating expandable table view cells
Swift
40
star
3

AVFoundation-Combine

Combine extensions for AVFoundation
Swift
30
star
4

swift-lessons

A collection of small tutorial apps using iOS 8 + Swift
Swift
9
star
5

TableViewExpansions

Expandable table view cells using dynamic cell insertion/removal
Swift
8
star
6

CustomTransitionsDemo

Example code for custom transitions in iOS
Swift
6
star
7

JVRCoreDataCollectionViewDataSource

A reusable class that combines UICollectionViewDataSource and NSFetchedResultsControllerDelegate
Objective-C
3
star
8

CocoaComponents

A collection of small components to help macOS development.
Swift
2
star
9

Annotator

A command line tool for annotating class extensions
Swift
2
star
10

ViewModelExtensions

Convenience library to inject view models into view controllers defined in Interface Builder.
Swift
2
star
11

JVRPersistentStack

An easy-to-use persistent stack class for apps using Core Data
Objective-C
2
star
12

AppExtensionsDemo

Example project for App Extensions in iOS 8
Swift
1
star
13

MVVMExample

Basic project to demonstrate the use of dependency injection and MVVM
Swift
1
star
14

swiftui-fun

A collection of small projects for learning SwiftUI
Swift
1
star
15

JVEReactiveItemDownloader

Test project for downloading and displaying events using ReactiveCocoa
Objective-C
1
star
16

jozsef-vesza.github.io

Source code for my blog, where I cover various topics related to iOS development.
SCSS
1
star
17

CatKit

Your placeholders just got furrier!
Swift
1
star
18

soappservices

Servlets for thesis application
Java
1
star
19

JVRBaseTableViewDataSource

A basic, reusable and expandable UITableViewDataSource class
Objective-C
1
star
20

JVRCellConfiguratorDelegate

A protocol for classes responsible for the creation and configuration of UICollectionViewCell or UITableViewCell objects.
Objective-C
1
star
21

JVRCoreDataCellHelper

Helper delegate protocol implemented by the class responsible for the configuration of UICollectionViewCell and UITableViewCell objects and the handling of item deletion
Objective-C
1
star
22

algorithms

Objective-C
1
star