ios - Getting autocomplete to work in swift -



ios - Getting autocomplete to work in swift -

i trying implement autocompletion, can't find illustration works in swift. below, i'm tring convert ray wenderlich's autocompletion tutorial , illustration code 2010. finally, code compiles, table containing possible completions not appear, , don't have experience see why not unhidden shouldchangecharactersinrange.

class viewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource, uitextfielddelegate { @iboutlet weak var textfield: uitextfield! allow autocompletetableview = uitableview(frame: cgrectmake(0,80,320,120), style: uitableviewstyle.plain) var pasturls = ["men", "women", "cats", "dogs", "children"] var autocompleteurls = [string]() override func viewdidload() { super.viewdidload() 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 var substring = (textfield.text nsstring).stringbyreplacingcharactersinrange(range, withstring: string) searchautocompleteentrieswithsubstring(substring) homecoming true // not sure - false } func searchautocompleteentrieswithsubstring(substring: string) { autocompleteurls.removeall(keepcapacity: false) var indexofpasturls = 0 curstring in pasturls { allow substringrange = curstring.rangeofstring(curstring) if (indexofpasturls == 0) { autocompleteurls.append(curstring) } indexofpasturls = indexofpasturls + 1 } autocompletetableview.reloaddata() } override func didreceivememorywarning() { super.didreceivememorywarning() } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { homecoming autocompleteurls.count } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { allow autocompleterowidentifier = "autocompleterowidentifier" var cell : uitableviewcell = tableview.dequeuereusablecellwithidentifier(autocompleterowidentifier, forindexpath: indexpath) uitableviewcell allow index = indexpath.row int cell.textlabel.text = autocompleteurls[index] homecoming cell } func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { allow selectedcell : uitableviewcell = tableview.cellforrowatindexpath(indexpath)! textfield.text = selectedcell.textlabel.text } }

replace searchautocompleteentrieswithsubstring function content 1 below. hope help you.

func searchautocompleteentrieswithsubstring(substring: string) { autocompleteurls.removeall(keepcapacity: false) curstring in pasturls { var mystring:nsstring! = curstring nsstring var substringrange :nsrange! = mystring.rangeofstring(substring) if (substringrange.location == 0) { autocompleteurls.append(curstring) } } autocompletetableview.reloaddata() }

ios swift autocomplete

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -