ios - Implementing a Search Text Box in Swift -


would able following in swift:

  • i have created textfield. when click on textfield displays table list
    • i have created func searches through array , matches added filtered array how add table -also there way when select new value table populates textfield?

i'm trying create dropdown

this code

import uikit  var names = ["frank", "john", "harry", "mike", "jones"] var filtered:[string] = []  class viewcontroller: uiviewcontroller {      var searchactive : bool = false     @iboutlet weak var searchtext: uitextfield!     @iboutlet weak var persontable: uitableview!      override func viewdidload() {         super.viewdidload()         persontable.alpha = 0         // additional setup after loading view, typically nib.     }      override func didreceivememorywarning() {         super.didreceivememorywarning()         // dispose of resources can recreated.     }      @ibaction func searchtextclick(sender: anyobject) {          if(persontable.alpha == 0){             persontable.alpha = 1             searchactive = true;             searchtext(searchtext, textdidchange: "")          }else{             persontable.alpha = 0             searchactive = false;         }     }      func tableview(persontable: uitableview!, numberofrowsinsection section: int) -> int     {         return names.count;     }       func tableview(persontable: uitableview!,     cellforrowatindexpath indexpath: nsindexpath!) -> uitableviewcell!     {         let cell:uitableviewcell = uitableviewcell(style:uitableviewcellstyle.default, reuseidentifier:"cell")         cell.textlabel!.text = names[indexpath.row]          return cell     }       func searchtext(searchtext: uitextfield, textdidchange newsearch: string) {         let filteredarr = names.filter{$0.containscharacters(searchtext.text)}     self.persontable.reloaddata()     }   }  extension string {     func containscharacters(ch:string)-> bool{          var err:nserror?          let reg = nsregularexpression(pattern: ch, options: nsregularexpressionoptions.caseinsensitive, error: &err)          if let matches = reg?.matchesinstring(self, options: nsmatchingoptions.withoutanchoringbounds, range: nsmakerange(0,count(self))) matches.count != 0 {              return true          }         return false     }  } 

thx

for reasons drop down's not exist in ios. should use uipickerview or add add search field tableview. in latter example should implement uisearchbardelegate , use 1 of it's delegate methods actions. this

func searchbar(searchbar: uisearchbar, textdidchange searchtext: string) {      if searchtext == "" {         self.filtereddkstationskeys = self.alldkstationskeys     }else{         self.filtereddkstationskeys.removeall(keepcapacity: false)         self.filtereddkstationskeys =  self.alldkstationskeys.filter { (  $0.lowercasestring.hasprefix(searchtext.lowercasestring) )  }     }     self.tableview.reloaddata() } 

where can replace filter stuff own filter functions (i show filtereddkstations in table). give table getting smaller , smaller less , less text matches search input. looks great!


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -