Skip to content
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

Fixed Swift Demo bugs #6

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
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/
43 changes: 25 additions & 18 deletions Swift/MPGTextField-Swift/MPGTextField-Swift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import UIKit

class ViewController: UIViewController, MPGTextFieldDelegate {

var sampleData = Dictionary<String, AnyObject>[]()
@IBOutlet var name : MPGTextField_Swift
var sampleData = [Dictionary<String, AnyObject>]()
@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() {
Expand All @@ -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<contents.count;++i{
var name = contents[i]["first_name"] as String
var lName = contents[i]["last_name"] as String
name += " " + lName
var email = contents[i]["email"] as String
var dictionary = ["DisplayText":name,"DisplaySubText":email,"CustomObject":contents[i]]

sampleData.append(dictionary)
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
var err: NSErrorPointer = NSErrorPointer()
var dataPath = NSBundle.mainBundle().pathForResource("sample_data", ofType: "json")
var data = NSData(contentsOfFile:dataPath!, options:NSDataReadingOptions.DataReadingUncached, error:err)

var contents: [AnyObject]! = NSJSONSerialization.JSONObjectWithData(data!,
options: NSJSONReadingOptions.AllowFragments,
error: err) as [AnyObject]

dispatch_async(dispatch_get_main_queue()) {
for item in contents {
var name = item["first_name"] as String
var lName = item["last_name"] as String
name += " " + lName
var email = item["email"] as String
var dictionary = ["DisplayText":name, "DisplaySubText":email, "CustomObject":item]

self.sampleData.append(dictionary)
}
}

}
}

func dataForPopoverInTextField(textfield: MPGTextField_Swift) -> Dictionary<String, AnyObject>[]
func dataForPopoverInTextField(textfield: MPGTextField_Swift) -> [Dictionary<String, AnyObject>]
{
return sampleData
}
Expand Down
30 changes: 15 additions & 15 deletions Swift/MPGTextField/MPGTextField-Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
import UIKit

@objc protocol MPGTextFieldDelegate{
func dataForPopoverInTextField(textfield: MPGTextField_Swift) -> Dictionary<String, AnyObject>[]
func dataForPopoverInTextField(textfield: MPGTextField_Swift) -> [Dictionary<String, AnyObject>]

@optional func textFieldDidEndEditing(textField: MPGTextField_Swift, withSelection data: Dictionary<String,AnyObject>)
@optional func textFieldShouldSelect(textField: MPGTextField_Swift) -> Bool
optional func textFieldDidEndEditing(textField: MPGTextField_Swift, withSelection data: Dictionary<String,AnyObject>)
optional func textFieldShouldSelect(textField: MPGTextField_Swift) -> Bool
}

class MPGTextField_Swift: UITextField, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate {

var mDelegate : MPGTextFieldDelegate?
var tableViewController : UITableViewController?
var data = Dictionary<String, AnyObject>[]()
var data = [Dictionary<String, AnyObject>]()

//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)
Expand All @@ -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)
}

Expand All @@ -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()
}
Expand Down Expand Up @@ -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
Expand All @@ -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: ({
Expand All @@ -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,
Expand All @@ -163,16 +163,16 @@ 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")
}

cell!.backgroundColor = UIColor.clearColor()
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!
}
Expand All @@ -185,7 +185,7 @@ class MPGTextField_Swift: UITextField, UITextFieldDelegate, UITableViewDelegate,

// #pragma mark Filter Method

func applyFilterWithSearchQuery(filter : String) -> Dictionary<String, AnyObject>[]
func applyFilterWithSearchQuery(filter : String) -> [Dictionary<String, AnyObject>]
{
//let predicate = NSPredicate(format: "DisplayText BEGINSWITH[cd] \(filter)")
var lower = (filter as NSString).lowercaseString
Expand All @@ -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"]
Expand Down