Skip to content

Rearrange cells in table view / Xcode 7 and Swift 2 support #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions WatchNotes WatchKit Extension/InterfaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class InterfaceController: WKInterfaceController,NSFilePresenter {

var notes :NSMutableArray?

override init(context: AnyObject?) {
override init() {
// Initialize variables here.
super.init(context: context)
super.init()

NSFileCoordinator.addFilePresenter(self)

Expand All @@ -45,7 +45,7 @@ class InterfaceController: WKInterfaceController,NSFilePresenter {
let fileCoordinator = NSFileCoordinator()
var note :String?

fileCoordinator.coordinateReadingItemAtURL(presentedItemURL!, options: nil, error: nil) { (newURL :NSURL!) -> Void in
fileCoordinator.coordinateReadingItemAtURL(presentedItemURL!, options: [], error: nil) { (newURL :NSURL) -> Void in

let savedData = NSData(contentsOfURL: newURL)

Expand All @@ -58,9 +58,9 @@ class InterfaceController: WKInterfaceController,NSFilePresenter {

self.tableView?.setNumberOfRows(self.notes!.count, withRowType: "NotesTableRowController")

for (index,value) in enumerate(self.notes!) {
for (index,value) in (self.notes!).enumerate() {

let row = self.tableView?.rowControllerAtIndex(index) as NotesTableRowController
let row = self.tableView?.rowControllerAtIndex(index) as! NotesTableRowController

row.noteTitleLabel?.setText(value as? String)

Expand Down
2 changes: 2 additions & 0 deletions WatchNotes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@
34E77AC21A211A150083241E /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftMigration = 0710;
LastSwiftUpdateCheck = 0710;
LastUpgradeCheck = 0620;
ORGANIZATIONNAME = "AzamSharp Consulting LLC";
TargetAttributes = {
Expand Down
27 changes: 24 additions & 3 deletions WatchNotes/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6221" systemVersion="13F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="mB2-wG-xz2">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9059" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="mB2-wG-xz2">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6213"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9049"/>
<capability name="Alignment constraints with different attributes" minToolsVersion="5.1"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
<scenes>
Expand All @@ -10,22 +12,37 @@
<objects>
<tableViewController id="m4A-cC-h09" customClass="NotesTableViewController" customModule="WatchNotes" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="8Ed-c0-cvO">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="NoteCell" id="vis-4Y-f1i">
<rect key="frame" x="0.0" y="86" width="600" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="vis-4Y-f1i" id="TbV-Mt-aPJ">
<rect key="frame" x="0.0" y="0.0" width="600" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<animations/>
</tableViewCellContentView>
<animations/>
</tableViewCell>
</prototypes>
<connections>
<outlet property="dataSource" destination="m4A-cC-h09" id="Tux-bp-1xs"/>
<outlet property="delegate" destination="m4A-cC-h09" id="f20-pC-g18"/>
</connections>
</tableView>
<navigationItem key="navigationItem" title="Notes" id="b2c-eN-xRm"/>
<navigationItem key="navigationItem" title="Notes" id="b2c-eN-xRm">
<barButtonItem key="rightBarButtonItem" title="Edit" id="2NB-8p-C3T">
<connections>
<action selector="beginEditing:" destination="m4A-cC-h09" id="whp-Rj-Trc"/>
</connections>
</barButtonItem>
</navigationItem>
<connections>
<outlet property="editButton" destination="2NB-8p-C3T" id="9ki-6I-4Ia"/>
</connections>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="0P4-UG-Ybc" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
Expand All @@ -45,11 +62,13 @@
<subviews>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="MEb-BV-6Q1">
<rect key="frame" x="0.0" y="-30" width="97" height="30"/>
<animations/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="3Gl-n5-trr">
<rect key="frame" x="-23" y="-15" width="46" height="30"/>
<animations/>
<state key="normal" title="Save">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
Expand All @@ -58,6 +77,7 @@
</connections>
</button>
</subviews>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="3Gl-n5-trr" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="177" id="5LC-Te-6K6"/>
Expand Down Expand Up @@ -108,6 +128,7 @@
<navigationBar key="navigationBar" contentMode="scaleToFill" id="yHI-kC-vzz">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<animations/>
</navigationBar>
<nil name="viewControllers"/>
<connections>
Expand Down
38 changes: 30 additions & 8 deletions WatchNotes/NotesTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@ class NotesTableViewController: UITableViewController,UITextFieldDelegate,NSFile
var notes :NSMutableArray = NSMutableArray()
var fileCoordinator = NSFileCoordinator()

@IBOutlet weak var editButton: UIBarButtonItem!

//When 'Edit' button is click in NavBar to rearrange table
@IBAction func beginEditing(sender: AnyObject) {
if(!self.editing){
editButton.title = "Done"
}else{
editButton.title = "Edit"
}
self.editing = !self.editing
}

override func viewDidLoad() {

super.viewDidLoad()


// delete all existing notes
// delete all existing notes
deleteAllNotes()

// populate the notes and update the tableView
Expand All @@ -43,7 +55,7 @@ class NotesTableViewController: UITableViewController,UITextFieldDelegate,NSFile
private func populateNotes() {

// get the notes array
fileCoordinator.coordinateReadingItemAtURL(presentedItemURL!, options: nil, error: nil) { (newURL :NSURL!) -> Void in
fileCoordinator.coordinateReadingItemAtURL(presentedItemURL!, options: [], error: nil) { (newURL :NSURL) -> Void in

let savedData = NSData(contentsOfURL: newURL)

Expand All @@ -52,7 +64,7 @@ class NotesTableViewController: UITableViewController,UITextFieldDelegate,NSFile
}
else {

self.notes = NSKeyedUnarchiver.unarchiveObjectWithData(savedData!) as NSMutableArray
self.notes = NSKeyedUnarchiver.unarchiveObjectWithData(savedData!) as! NSMutableArray
}

}
Expand All @@ -61,7 +73,7 @@ class NotesTableViewController: UITableViewController,UITextFieldDelegate,NSFile
private func saveNote(note :String) {

// write note into the notes array
fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: nil, error: nil) { ( newURL :NSURL!) -> Void in
fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: [], error: nil) { ( newURL :NSURL) -> Void in

self.notes.addObject(note)

Expand All @@ -74,7 +86,7 @@ class NotesTableViewController: UITableViewController,UITextFieldDelegate,NSFile
private func updateNotes() {

// write note into the notes array
fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: nil, error: nil) { ( newURL :NSURL!) -> Void in
fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: [], error: nil) { ( newURL :NSURL) -> Void in

let saveData = NSKeyedArchiver.archivedDataWithRootObject(self.notes)
let success = saveData.writeToURL(newURL, atomically: true)
Expand All @@ -84,7 +96,7 @@ class NotesTableViewController: UITableViewController,UITextFieldDelegate,NSFile

private func deleteAllNotes() {

fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: nil, error: nil) { ( newURL :NSURL!) -> Void in
fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: [], error: nil) { ( newURL :NSURL) -> Void in

self.notes = NSMutableArray() // writing an empty array

Expand Down Expand Up @@ -142,7 +154,7 @@ class NotesTableViewController: UITableViewController,UITextFieldDelegate,NSFile

let cell = tableView.dequeueReusableCellWithIdentifier("NoteCell", forIndexPath: indexPath) as UITableViewCell

var note = self.notes[indexPath.row] as String
var note = self.notes[indexPath.row] as! String

cell.textLabel?.text = note

Expand All @@ -153,6 +165,16 @@ class NotesTableViewController: UITableViewController,UITextFieldDelegate,NSFile
return true;
}

override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true}

override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
var itemToMove = notes[fromIndexPath.row]
notes.removeObjectAtIndex(fromIndexPath.row)
notes.insertObject(itemToMove, atIndex: toIndexPath.row)
updateNotes()
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

if editingStyle == UITableViewCellEditingStyle.Delete {
Expand All @@ -167,7 +189,7 @@ class NotesTableViewController: UITableViewController,UITextFieldDelegate,NSFile

func textFieldDidEndEditing(textField: UITextField) {

saveNote(textField.text)
saveNote(textField.text!)

// reload table
self.tableView.reloadData()
Expand Down
6 changes: 3 additions & 3 deletions WatchNotes/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ViewController: UIViewController,NSFilePresenter {

let fileCoordinator = NSFileCoordinator()

fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: nil, error: nil) { ( newURL :NSURL!) -> Void in
fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: [], error: nil) { ( newURL :NSURL) -> Void in

self.notes = NSMutableArray() // writing an empty array

Expand All @@ -61,7 +61,7 @@ class ViewController: UIViewController,NSFilePresenter {
let note = self.noteTextField?.text

// get the notes array
fileCoordinator.coordinateReadingItemAtURL(presentedItemURL!, options: nil, error: nil) { (newURL :NSURL!) -> Void in
fileCoordinator.coordinateReadingItemAtURL(presentedItemURL!, options: [], error: nil) { (newURL :NSURL) -> Void in

let savedData = NSData(contentsOfURL: newURL)

Expand All @@ -77,7 +77,7 @@ class ViewController: UIViewController,NSFilePresenter {


// write note into the notes array
fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: nil, error: nil) { ( newURL :NSURL!) -> Void in
fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: [], error: nil) { ( newURL :NSURL) -> Void in

self.notes?.addObject(note!)

Expand Down