Skip to content

Instantly share code, notes, and snippets.

@uliwitness
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uliwitness/6c065f90362e83b293f3 to your computer and use it in GitHub Desktop.
Save uliwitness/6c065f90362e83b293f3 to your computer and use it in GitHub Desktop.
Most iOS apps have a static main table showing the different sections of the application. Sometimes you have different variants of the same app (e.g. Lite and full, or branded versions) that use the same sources. Would be nice to just be able to #ifdef out sections one version doesn't need. This is an attempt at Swiftifying the ObjC pattern I us…
//
// ViewController.swift
// SwiftTableTestIOS
//
import UIKit
class ViewController: UITableViewController {
// These enum cases should be in the order the user should see the items in:
enum ERowEnum : Int
{
case Foo
case Bar
case Baz
case Boff
case _Last // Count of visible items. Move a constant behind this to hide its section.
}
// Text keys matching an item (e.g. for looking up localized title or image for an item). Could be more complex type.
let rows : Dictionary<ERowEnum,String> = [ .Foo: "foo", .Bar: "bar", .Baz: "baz", .Boff: "boff" ];
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ERowEnum._Last.rawValue // Number of items, as indexes start at 0.
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier( "com.thevoidsoftware.swifttabletestios.maincell", forIndexPath: indexPath ) as UITableViewCell
cell.textLabel!.text = rows[ERowEnum(rawValue: indexPath.row)!] // Look up right row entry (title in this simple example) for this row.
return cell;
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
switch( ERowEnum(rawValue: indexPath.row)! )
{
case .Foo: // React to tap in particular row.
println( "Foo" )
default:
println( "Someplace else tapped." )
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment