From d1be9e527bc30e60ede8d3cf9d3c838c138a0c4b Mon Sep 17 00:00:00 2001 From: fantasyczl Date: Wed, 26 Nov 2014 12:49:34 +0800 Subject: [PATCH 1/2] add gitignore --- .gitignore | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b00804d --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +.DS_Store +build/ +*.pbxuser +!default.pbxuser +*.mode1v3 +!default.mode1v3 +*.mode2v3 +!default.mode2v3 +*.perspectivev3 +!default.perspectivev3 +*.xcworkspace +!default.xcworkspace +xcuserdata +profile +*.moved-aside +DerivedData +.idea/ From ee5c9890ccc92ecf64aca3c1cfabe5482137f01f Mon Sep 17 00:00:00 2001 From: fantasyczl Date: Wed, 26 Nov 2014 14:38:14 +0800 Subject: [PATCH 2/2] fixed error in swift project --- .../MPGTextField-Swift/ViewController.swift | 43 +++++++++++-------- Swift/MPGTextField/MPGTextField-Swift.swift | 30 ++++++------- 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/Swift/MPGTextField-Swift/MPGTextField-Swift/ViewController.swift b/Swift/MPGTextField-Swift/MPGTextField-Swift/ViewController.swift index b4bbf4e..cd6671a 100644 --- a/Swift/MPGTextField-Swift/MPGTextField-Swift/ViewController.swift +++ b/Swift/MPGTextField-Swift/MPGTextField-Swift/ViewController.swift @@ -10,14 +10,14 @@ import UIKit class ViewController: UIViewController, MPGTextFieldDelegate { - var sampleData = Dictionary[]() - @IBOutlet var name : MPGTextField_Swift + var sampleData = [Dictionary]() + @IBOutlet var name: MPGTextField_Swift? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.generateData() - name.mDelegate = self + name!.mDelegate = self } override func didReceiveMemoryWarning() { @@ -26,24 +26,31 @@ class ViewController: UIViewController, MPGTextFieldDelegate { } func generateData(){ - var err : NSErrorPointer? - var dataPath = NSBundle.mainBundle().pathForResource("sample_data", ofType: "json") - var data = NSData.dataWithContentsOfFile(dataPath, options: NSDataReadingOptions.DataReadingUncached, error: err!) - var contents : AnyObject[]! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: err!) as AnyObject[] - //println(contents[0]["first_name"]) - for var i = 0;i Dictionary[] + func dataForPopoverInTextField(textfield: MPGTextField_Swift) -> [Dictionary] { return sampleData } diff --git a/Swift/MPGTextField/MPGTextField-Swift.swift b/Swift/MPGTextField/MPGTextField-Swift.swift index 26580c1..fbc38b4 100644 --- a/Swift/MPGTextField/MPGTextField-Swift.swift +++ b/Swift/MPGTextField/MPGTextField-Swift.swift @@ -9,17 +9,17 @@ import UIKit @objc protocol MPGTextFieldDelegate{ - func dataForPopoverInTextField(textfield: MPGTextField_Swift) -> Dictionary[] + func dataForPopoverInTextField(textfield: MPGTextField_Swift) -> [Dictionary] - @optional func textFieldDidEndEditing(textField: MPGTextField_Swift, withSelection data: Dictionary) - @optional func textFieldShouldSelect(textField: MPGTextField_Swift) -> Bool + optional func textFieldDidEndEditing(textField: MPGTextField_Swift, withSelection data: Dictionary) + optional func textFieldShouldSelect(textField: MPGTextField_Swift) -> Bool } class MPGTextField_Swift: UITextField, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate { var mDelegate : MPGTextFieldDelegate? var tableViewController : UITableViewController? - var data = Dictionary[]() + var data = [Dictionary]() //Set this to override the default color of suggestions popover. The default color is [UIColor colorWithWhite:0.8 alpha:0.9] @IBInspectable var popoverBackgroundColor : UIColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 1.0) @@ -31,12 +31,12 @@ class MPGTextField_Swift: UITextField, UITextFieldDelegate, UITableViewDelegate, @IBInspectable var seperatorColor : UIColor = UIColor(white: 0.95, alpha: 1.0) - init(frame: CGRect) { + override init(frame: CGRect) { super.init(frame: frame) // Initialization code } - init(coder aDecoder: NSCoder!){ + required init(coder aDecoder: NSCoder){ super.init(coder: aDecoder) } @@ -55,7 +55,7 @@ class MPGTextField_Swift: UITextField, UITextFieldDelegate, UITableViewDelegate, if (countElements(str) > 0) && (self.isFirstResponder()) { - if mDelegate{ + if mDelegate != nil { data = mDelegate!.dataForPopoverInTextField(self) self.provideSuggestions() } @@ -97,7 +97,7 @@ class MPGTextField_Swift: UITextField, UITextFieldDelegate, UITableViewDelegate, tapRecognizer.numberOfTapsRequired = 1 tapRecognizer.cancelsTouchesInView = false tapRecognizer.delegate = self - self.superview.addGestureRecognizer(tapRecognizer) + self.superview!.addGestureRecognizer(tapRecognizer) self.tableViewController = UITableViewController.alloc() self.tableViewController!.tableView.delegate = self @@ -120,7 +120,7 @@ class MPGTextField_Swift: UITextField, UITextFieldDelegate, UITableViewDelegate, frameForPresentation.size.height = 200; tableViewController!.tableView.frame = frameForPresentation - self.superview.addSubview(tableViewController!.tableView) + self.superview!.addSubview(tableViewController!.tableView) self.tableViewController!.tableView.alpha = 0.0 UIView.animateWithDuration(0.3, animations: ({ @@ -142,7 +142,7 @@ class MPGTextField_Swift: UITextField, UITextFieldDelegate, UITableViewDelegate, } } - func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{ + func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ var count = self.applyFilterWithSearchQuery(self.text).count if count == 0{ UIView.animateWithDuration(0.3, @@ -163,7 +163,7 @@ class MPGTextField_Swift: UITextField, UITextFieldDelegate, UITableViewDelegate, func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("MPGResultsCell") as? UITableViewCell - if !cell{ + if cell == nil { cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MPGResultsCell") } @@ -171,8 +171,8 @@ class MPGTextField_Swift: UITextField, UITextFieldDelegate, UITableViewDelegate, let dataForRowAtIndexPath = self.applyFilterWithSearchQuery(self.text)[indexPath.row] let displayText : AnyObject? = dataForRowAtIndexPath["DisplayText"] let displaySubText : AnyObject? = dataForRowAtIndexPath["DisplaySubText"] - cell!.textLabel.text = displayText as String - cell!.detailTextLabel.text = displaySubText as String + cell!.textLabel.text = displayText as? String + cell!.detailTextLabel!.text = displaySubText as? String return cell! } @@ -185,7 +185,7 @@ class MPGTextField_Swift: UITextField, UITextFieldDelegate, UITableViewDelegate, // #pragma mark Filter Method - func applyFilterWithSearchQuery(filter : String) -> Dictionary[] + func applyFilterWithSearchQuery(filter : String) -> [Dictionary] { //let predicate = NSPredicate(format: "DisplayText BEGINSWITH[cd] \(filter)") var lower = (filter as NSString).lowercaseString @@ -205,7 +205,7 @@ class MPGTextField_Swift: UITextField, UITextFieldDelegate, UITableViewDelegate, if let table = self.tableViewController{ table.tableView.removeFromSuperview() } - if mDelegate?.textFieldShouldSelect?(self){ + if mDelegate!.textFieldShouldSelect!(self){ if self.applyFilterWithSearchQuery(self.text).count > 0 { let selectedData = self.applyFilterWithSearchQuery(self.text)[0] let displayText : AnyObject? = selectedData["DisplayText"]