Skip to content

yazid/Swift-Tables

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Swift Tables

About 180 pages into the Swift Programming Language iBook, I got a little bored and wanted to throw myself into a pool of Swift already. I wanted to do this the same way I did with Objective-C all those years back: by building a good ol' UITableView setup. This setup is good because it has a lot of the basics:

  • Setting up and manipulating arrays
  • Hooking up IBOutlets and IBActions
  • Using Storyboards
  • Conforming to existing protocols
  • Setting up and conforming to your own protocols
  • Displaying alerts

With the exception of touching into persistent storage, the above is arguably enough to build some very basic functional apps. So what does the sample do?

  • First view displays the elements of an array property onto a UITableView
  • When selecting a cell, the second view gets pushed onto the UINavigationController
  • An alert is displayed informing of the index of the selected row from the first view
  • Write a value into the UITextField, click the button, and the second view gets popped off
  • Upon returning to the first view, the UITextField value of the second view is displayed in another alert, and replaces the label of the previously selected row

Things I Learnt the First Time I Coded in Swift

This was the absolute first time I coded anything in Swift so I took some notes along the way.

General

  • There are no more @s in front of strings. No more ;s at the end of lines. No .h and .m files, just a single .swift file! Yay yay Yay?
  • Swift autocomplete in Xcode 6 beta is a little wonky.
  • I could be completely off with any of these points so do correct me if I'm wrong!

Arrays

  • Declaring an array property and initializing it:

    var dataSource = String[]()
    
  • Adding elements:

    dataSource.append("Element 1")
    
  • Replacing elements:

    dataSource[tableView.indexPathForSelectedRow().row] = string
    

Storyboards/IBOutlet/IBAction

  • Setting up an IBOutlet in Swift (just put the tag above your property declaration):

    @IBOutlet
    var tableView: UITableView
    
  • There's no need to do a #import "SomeOtherClass.h" to access those classes anymore. This was apparent when I was trying to cast segue.destinationViewController as my custom class in prepareForSegue so I could access the properties I had setup in it, but turns out all I had to do was directly cast it like so:

    var destinationVC:DetailViewController = segue.destinationViewController as DetailViewController
    

    Neat!

  • If your destinationViewController class needs some custom properties, you may want to make them optional (using question marks at the end of the property declaration, e.g. var delegate: ViewController?). Otherwise, the compiler is going to complain with an error that those properties are not initialized in super.init. If it's possible, setup your inits accordingly.

  • If you're reaching another scene via a segue, the View Controller of that other scene needs to at least have a boilerplate init(coder) method or you're going to get a compiler error (more details here). Here's what that looks like:

    init(coder aDecoder: NSCoder!){
        super.init(coder: aDecoder)
    }
    
  • Setting up an IBAction in Swift:

    @IBAction func dismissButtonPressed() {
        ...
    }
    

Protocols

  • To declare a custom protocol, you do this above your class definition:

    protocol DetailViewProtocol {
        func didFinishDetailView(string:String)
    }
    
  • Conforming to protocols is pretty easy. Just list them out separated by commas right there at your class definition:

    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, DetailViewProtocol {
        ...
    }
    

    No need for angle brackets!

Alerts

  • The UIAlertView has been deprecated in iOS 8. You can now display alerts as such:

    var alert = UIAlertController(title: "Alert", message: "You typed \"\(string)\"", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
    

Of course all this is really basic stuff and it doesn't even begin to touch on the real power of Swift, but it's just enough to get a feel for it if you get tired of reading code (instead of writing it).

Swift programming ahead!

About

A starter point to get into Swift with a simple UITableView-based project in iOS 8

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages