8
8
9
9
import UIKit
10
10
11
- class mainTableViewController : UITableViewController {
11
+ class mainTableViewController : UITableViewController , UITextFieldDelegate {
12
12
13
- let calculatorCurrencyIdentifier = " Calculator_Currency "
13
+ //MARK: - property
14
14
15
+ let calculatorCurrencyIdentifier = " Calculator_Currency "
16
+ var notificationCenter : NSNotificationCenter = NSNotificationCenter . defaultCenter ( )
15
17
var currencyItemsList : NSMutableArray = [ ]
16
18
19
+ var selectedRow = - 1
20
+ var selectedRow_Currency : CurrencyItem !
21
+ var baseMoneyInUSD : Float !
22
+
23
+
17
24
override func viewDidLoad( ) {
18
25
super. viewDidLoad ( )
19
26
@@ -23,11 +30,13 @@ class mainTableViewController: UITableViewController {
23
30
let currencyItem_USD : CurrencyItem = CurrencyItem ( flatName: " US " , shortName: " USD " , fullName: " 美元 " , price: 1.00 )
24
31
currencyItemsList. addObject ( currencyItem_USD)
25
32
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 )
27
34
currencyItemsList. addObject ( currencyItem_EUR)
28
35
29
36
}
30
37
38
+ //MARK: - tableViewDelegate
39
+
31
40
override func tableView( tableView: UITableView , numberOfRowsInSection section: Int ) -> Int {
32
41
return currencyItemsList. count
33
42
}
@@ -40,14 +49,20 @@ class mainTableViewController: UITableViewController {
40
49
let cell = tableView. dequeueReusableCellWithIdentifier ( calculatorCurrencyIdentifier, forIndexPath: indexPath) as! UITableViewCell
41
50
var currencyForCell = currencyItemsList. objectAtIndex ( indexPath. row) as! CurrencyItem
42
51
43
-
44
52
var shortName = cell. viewWithTag ( 200 ) as! UILabel
45
53
shortName. text = currencyForCell. currencyShortName
46
54
47
55
var fullName = cell. viewWithTag ( 300 ) as! UILabel
48
56
fullName. text = currencyForCell. currencyFullName
49
57
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 ( )
51
66
52
67
return cell
53
68
}
@@ -59,14 +74,48 @@ class mainTableViewController: UITableViewController {
59
74
}
60
75
61
76
override func tableView( tableView: UITableView , didSelectRowAtIndexPath indexPath: NSIndexPath ) {
62
- tableView. deselectRowAtIndexPath ( indexPath, animated: true )
63
77
64
- let cell = tableView. dequeueReusableCellWithIdentifier ( calculatorCurrencyIdentifier, forIndexPath: indexPath) as! UITableViewCell
78
+ selectedRow = indexPath. row
79
+
80
+ let cell : UITableViewCell = tableView. cellForRowAtIndexPath ( indexPath) !
65
81
var textField = cell. viewWithTag ( 400 ) as! UITextField
82
+ textField. placeholder = " 100 "
83
+ textField. text = " \( 100 ) "
84
+
85
+ refreshTabelViewCell ( textField)
86
+
66
87
textField. becomeFirstResponder ( )
88
+
89
+ notificationCenter. addObserver ( self , selector: " textField_Value_Changed: " , name: UITextFieldTextDidChangeNotification, object: textField)
90
+
91
+ tableView. deselectRowAtIndexPath ( indexPath, animated: true )
67
92
}
68
93
69
94
override func tableView( tableView: UITableView , heightForRowAtIndexPath indexPath: NSIndexPath ) -> CGFloat {
70
95
return 85
71
96
}
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
+
72
121
}
0 commit comments