Reputation: 153
in my iOS app (swift 2) i would like to realize a textfield with autocomplete suggestions. this works already fine.
my problem is the layout.
i have a table view controller with one section and 3 cells. in cell 1 is a container which includes the textfield with the table of suggestions. in cell 2 and 3 are only labels.
http://fs5.directupload.net/images/160128/vwaydpyc.png
This is the result:
http://fs5.directupload.net/images/160128/kz5bsurx.png
i would like to set the height of cell 1 as the same height of cell 2 and 3. and the suggestions should be "pop up" over all cells and not only in cell 1, where the rest will be not visible.
Have you any ideas?
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var textField: UITextField!
@IBOutlet var autocompleteTableView: UITableView!
var pastUrls = ["Men", "Women", "Cats", "Dogs", "Children"]
var autocompleteUrls = [String]()
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
autocompleteTableView!.delegate = self
autocompleteTableView!.dataSource = self
autocompleteTableView!.scrollEnabled = true
autocompleteTableView!.hidden = true
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
autocompleteTableView!.hidden = false
let substring = (self.textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
searchAutocompleteEntriesWithSubstring(substring)
return true
}
func searchAutocompleteEntriesWithSubstring(substring: String)
{
autocompleteUrls.removeAll(keepCapacity: false)
for curString in pastUrls
{
let myString: NSString! = curString as NSString
let substringRange: NSRange! = myString.rangeOfString(substring)
if (substringRange.location == 0)
{
autocompleteUrls.append(curString)
}
}
autocompleteTableView!.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return autocompleteUrls.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let autoCompleteRowIdentifier = "AutoCompleteRowIdentifier"
let cell = tableView.dequeueReusableCellWithIdentifier(autoCompleteRowIdentifier)
let index = indexPath.row as Int
cell!.textLabel!.text = autocompleteUrls[index]
return cell!
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedCell : UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
textField.text = selectedCell.textLabel!.text
}
}
Upvotes: 0
Views: 2141
Reputation: 1956
MPGTextField is a Swift version of third party Drop down text field library.
And for future reference you can find great thrid party controls at Cocoa controls
Upvotes: 1