Code Monkey home page Code Monkey logo

expandingstackcells's Introduction

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.

expandingstackcells's People

Contributors

jozsef-vesza avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.