Skip to content

Commit ff8a1f9

Browse files
author
JummyHuang
committed
add curreny calculator function when you put the money in one currency, it is automatically transformed in other type of curreny
1 parent eb1162d commit ff8a1f9

File tree

5 files changed

+129
-28
lines changed

5 files changed

+129
-28
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
6+
<false/>
7+
<key>IDESourceControlProjectIdentifier</key>
8+
<string>EF78F2F1-6031-4E3C-B0F0-BB437AAB2889</string>
9+
<key>IDESourceControlProjectName</key>
10+
<string>project</string>
11+
<key>IDESourceControlProjectOriginsDictionary</key>
12+
<dict>
13+
<key>3D0FE4354A7F4D669A5C80A1E4871340F88D6D03</key>
14+
<string>https://github.com/JummyHuang/Currency.git</string>
15+
</dict>
16+
<key>IDESourceControlProjectPath</key>
17+
<string>Currency.xcodeproj/project.xcworkspace</string>
18+
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
19+
<dict>
20+
<key>3D0FE4354A7F4D669A5C80A1E4871340F88D6D03</key>
21+
<string>../..</string>
22+
</dict>
23+
<key>IDESourceControlProjectURL</key>
24+
<string>https://github.com/JummyHuang/Currency.git</string>
25+
<key>IDESourceControlProjectVersion</key>
26+
<integer>111</integer>
27+
<key>IDESourceControlProjectWCCIdentifier</key>
28+
<string>3D0FE4354A7F4D669A5C80A1E4871340F88D6D03</string>
29+
<key>IDESourceControlProjectWCConfigurations</key>
30+
<array>
31+
<dict>
32+
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
33+
<string>public.vcs.git</string>
34+
<key>IDESourceControlWCCIdentifierKey</key>
35+
<string>3D0FE4354A7F4D669A5C80A1E4871340F88D6D03</string>
36+
<key>IDESourceControlWCCName</key>
37+
<string>Currency</string>
38+
</dict>
39+
</array>
40+
</dict>
41+
</plist>

Currency.xcodeproj/xcuserdata/huangjunming.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,30 @@
3535
landmarkType = "5">
3636
</BreakpointContent>
3737
</BreakpointProxy>
38+
<BreakpointProxy
39+
BreakpointExtensionID = "Xcode.Breakpoint.ExceptionBreakpoint">
40+
<BreakpointContent
41+
shouldBeEnabled = "Yes"
42+
ignoreCount = "0"
43+
continueAfterRunningActions = "No"
44+
scope = "0"
45+
stopOnStyle = "0">
46+
</BreakpointContent>
47+
</BreakpointProxy>
3848
<BreakpointProxy
3949
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
4050
<BreakpointContent
4151
shouldBeEnabled = "No"
4252
ignoreCount = "0"
4353
continueAfterRunningActions = "No"
4454
filePath = "Currency/mainTableViewController.swift"
45-
timestampString = "459271298.675744"
55+
timestampString = "459525748.839597"
4656
startingColumnNumber = "9223372036854775807"
4757
endingColumnNumber = "9223372036854775807"
48-
startingLineNumber = "40"
49-
endingLineNumber = "40"
50-
landmarkName = "tableView(_:cellForRowAtIndexPath:)"
51-
landmarkType = "5">
58+
startingLineNumber = "36"
59+
endingLineNumber = "36"
60+
landmarkName = "mainTableViewController"
61+
landmarkType = "3">
5262
</BreakpointContent>
5363
</BreakpointProxy>
5464
</Breakpoints>

Currency/Base.lproj/Main.storyboard

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,36 @@
3232
<nil key="highlightedColor"/>
3333
</label>
3434
<label opaque="NO" userInteractionEnabled="NO" tag="300" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="FullName" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Viy-GT-ZCW">
35-
<rect key="frame" x="412" y="51" width="180" height="21"/>
35+
<rect key="frame" x="482" y="63" width="110" height="12"/>
3636
<constraints>
37-
<constraint firstAttribute="height" constant="21" id="A0Y-zz-kdY"/>
38-
<constraint firstAttribute="width" constant="180" id="MYt-z6-G5q"/>
37+
<constraint firstAttribute="width" constant="110" id="2sY-4i-Wsr"/>
38+
<constraint firstAttribute="height" constant="12" id="EfZ-Fz-Ja6"/>
3939
</constraints>
40-
<fontDescription key="fontDescription" type="system" pointSize="17"/>
40+
<fontDescription key="fontDescription" type="system" pointSize="12"/>
4141
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
4242
<nil key="highlightedColor"/>
4343
</label>
44-
<textField opaque="NO" clipsSubviews="YES" tag="400" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="请输入金额" textAlignment="right" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="Zmd-fR-Fxw">
45-
<rect key="frame" x="492" y="13" width="100" height="30"/>
44+
<textField opaque="NO" clipsSubviews="YES" tag="400" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" textAlignment="right" clearsOnBeginEditing="YES" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="Zmd-fR-Fxw">
45+
<rect key="frame" x="493" y="21" width="99" height="30"/>
4646
<constraints>
47-
<constraint firstAttribute="height" constant="30" id="J38-5B-5J7"/>
48-
<constraint firstAttribute="width" constant="100" id="hme-05-F8p"/>
47+
<constraint firstAttribute="width" constant="99" id="mIn-HD-ug6"/>
48+
<constraint firstAttribute="height" constant="30" id="xO7-SV-6ao"/>
4949
</constraints>
50-
<fontDescription key="fontDescription" type="system" pointSize="14"/>
51-
<textInputTraits key="textInputTraits" keyboardType="numberPad"/>
50+
<color key="textColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="calibratedRGB"/>
51+
<fontDescription key="fontDescription" type="system" pointSize="25"/>
52+
<textInputTraits key="textInputTraits" keyboardType="decimalPad" returnKeyType="done" enablesReturnKeyAutomatically="YES"/>
5253
</textField>
5354
</subviews>
5455
<constraints>
5556
<constraint firstItem="DRl-B1-cbt" firstAttribute="leading" secondItem="W4L-Fh-n26" secondAttribute="leading" constant="80" id="1Vy-Y3-bkc"/>
5657
<constraint firstItem="DRl-B1-cbt" firstAttribute="top" secondItem="W4L-Fh-n26" secondAttribute="top" constant="32" id="4hZ-Vj-Xqx"/>
57-
<constraint firstAttribute="bottom" secondItem="Viy-GT-ZCW" secondAttribute="bottom" constant="12.5" id="7PL-ee-vuV"/>
58-
<constraint firstAttribute="trailing" secondItem="Viy-GT-ZCW" secondAttribute="trailing" constant="8" id="CNA-jH-I4O"/>
59-
<constraint firstAttribute="trailing" secondItem="Zmd-fR-Fxw" secondAttribute="trailing" constant="8" id="HIf-UM-Ehm"/>
60-
<constraint firstItem="Viy-GT-ZCW" firstAttribute="top" secondItem="W4L-Fh-n26" secondAttribute="top" constant="51" id="N9T-Dk-bMI"/>
61-
<constraint firstAttribute="bottom" secondItem="Zmd-fR-Fxw" secondAttribute="bottom" constant="41.5" id="ZVH-ZH-SQA"/>
62-
<constraint firstItem="Zmd-fR-Fxw" firstAttribute="top" secondItem="W4L-Fh-n26" secondAttribute="top" constant="13" id="fXK-xF-pta"/>
58+
<constraint firstAttribute="trailing" secondItem="Zmd-fR-Fxw" secondAttribute="trailing" constant="8" id="Qnl-Yj-EVk"/>
59+
<constraint firstAttribute="bottom" secondItem="Zmd-fR-Fxw" secondAttribute="bottom" constant="33.5" id="RYA-HY-QGB"/>
60+
<constraint firstAttribute="bottom" secondItem="Viy-GT-ZCW" secondAttribute="bottom" constant="9.5" id="by9-ge-Lkq"/>
61+
<constraint firstItem="Zmd-fR-Fxw" firstAttribute="top" secondItem="W4L-Fh-n26" secondAttribute="top" constant="21" id="hIN-OE-k5u"/>
62+
<constraint firstAttribute="trailing" secondItem="Viy-GT-ZCW" secondAttribute="trailing" constant="8" id="htq-ue-gPF"/>
6363
<constraint firstAttribute="bottom" secondItem="DRl-B1-cbt" secondAttribute="bottom" constant="31.5" id="kNN-Gy-HIS"/>
64+
<constraint firstItem="Viy-GT-ZCW" firstAttribute="top" secondItem="W4L-Fh-n26" secondAttribute="top" constant="63" id="zXQ-ZH-8Ob"/>
6465
</constraints>
6566
</tableViewCellContentView>
6667
</tableViewCell>

Currency/mainTableViewController.swift

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@
88

99
import UIKit
1010

11-
class mainTableViewController: UITableViewController {
11+
class mainTableViewController: UITableViewController, UITextFieldDelegate {
1212

13-
let calculatorCurrencyIdentifier = "Calculator_Currency"
13+
//MARK: - property
1414

15+
let calculatorCurrencyIdentifier = "Calculator_Currency"
16+
var notificationCenter: NSNotificationCenter = NSNotificationCenter.defaultCenter()
1517
var currencyItemsList: NSMutableArray = []
1618

19+
var selectedRow = -1
20+
var selectedRow_Currency: CurrencyItem!
21+
var baseMoneyInUSD: Float!
22+
23+
1724
override func viewDidLoad() {
1825
super.viewDidLoad()
1926

@@ -23,11 +30,13 @@ class mainTableViewController: UITableViewController {
2330
let currencyItem_USD: CurrencyItem = CurrencyItem(flatName: "US", shortName: "USD", fullName: "美元", price: 1.00)
2431
currencyItemsList.addObject(currencyItem_USD)
2532

26-
let currencyItem_EUR: CurrencyItem = CurrencyItem(flatName: "EU", shortName: "EUR", fullName: "欧元", price: 1.09)
33+
let currencyItem_EUR: CurrencyItem = CurrencyItem(flatName: "EU", shortName: "EUR", fullName: "欧元", price: 0.9)
2734
currencyItemsList.addObject(currencyItem_EUR)
2835

2936
}
3037

38+
//MARK: - tableViewDelegate
39+
3140
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
3241
return currencyItemsList.count
3342
}
@@ -40,14 +49,20 @@ class mainTableViewController: UITableViewController {
4049
let cell = tableView.dequeueReusableCellWithIdentifier(calculatorCurrencyIdentifier, forIndexPath: indexPath) as! UITableViewCell
4150
var currencyForCell = currencyItemsList.objectAtIndex(indexPath.row) as! CurrencyItem
4251

43-
4452
var shortName = cell.viewWithTag(200) as! UILabel
4553
shortName.text = currencyForCell.currencyShortName
4654

4755
var fullName = cell.viewWithTag(300) as! UILabel
4856
fullName.text = currencyForCell.currencyFullName
4957

50-
print(fullName)
58+
var textField = cell.viewWithTag(400) as! UITextField
59+
60+
if selectedRow > -1 && selectedRow <= currencyItemsList.count {
61+
textField.text = String(format: "%.2f", currencyForCell.valueForTextField)
62+
} else {
63+
textField.text = "0.0"
64+
}
65+
textField.sizeToFit()
5166

5267
return cell
5368
}
@@ -59,14 +74,48 @@ class mainTableViewController: UITableViewController {
5974
}
6075

6176
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
62-
tableView.deselectRowAtIndexPath(indexPath, animated: true)
6377

64-
let cell = tableView.dequeueReusableCellWithIdentifier(calculatorCurrencyIdentifier, forIndexPath: indexPath) as! UITableViewCell
78+
selectedRow = indexPath.row
79+
80+
let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
6581
var textField = cell.viewWithTag(400) as! UITextField
82+
textField.placeholder = "100"
83+
textField.text = "\(100)"
84+
85+
refreshTabelViewCell(textField)
86+
6687
textField.becomeFirstResponder()
88+
89+
notificationCenter.addObserver(self, selector: "textField_Value_Changed:", name: UITextFieldTextDidChangeNotification, object: textField)
90+
91+
tableView.deselectRowAtIndexPath(indexPath, animated: true)
6792
}
6893

6994
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
7095
return 85
7196
}
97+
98+
//MARK: - custom function
99+
100+
func refreshTabelViewCell(textField: UITextField) {
101+
selectedRow_Currency = currencyItemsList.objectAtIndex(selectedRow) as! CurrencyItem
102+
selectedRow_Currency.valueForTextField = (textField.text as NSString).floatValue
103+
print("textField text is \(selectedRow_Currency.valueForTextField)")
104+
baseMoneyInUSD = selectedRow_Currency.valueForTextField / selectedRow_Currency.currencyPrice
105+
106+
for i in 0..<currencyItemsList.count {
107+
var currencyItem = currencyItemsList.objectAtIndex(i) as! CurrencyItem
108+
if i != selectedRow {
109+
currencyItem.valueForTextField = baseMoneyInUSD * currencyItem.currencyPrice
110+
let indexPath: NSIndexPath = NSIndexPath(forRow: i, inSection: 0)
111+
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
112+
}
113+
}
114+
}
115+
116+
func textField_Value_Changed(notification: NSNotification) {
117+
var textField: UITextField = notification.object as! UITextField
118+
refreshTabelViewCell(textField)
119+
}
120+
72121
}

0 commit comments

Comments
 (0)