获取自动完成在SWIFT工作 [英] Getting autocomplete to work in swift

查看:106
本文介绍了获取自动完成在SWIFT工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现自动完成,但无法找到,在斯威夫特工作的例子。下面,我特林转换<一个href=\"http://www.raywenderlich.com/336/auto-complete-tutorial-for-ios-how-to-auto-complete-with-custom-values\">Ray从2010年Wenderlich的自动完成教程以及例如code最后,code编译,但含有可能的补全表没有出现,我没有经验,看看它为什么不取消隐藏通过shouldChangeCharactersInRange。

 类的ViewController:UIViewController的,的UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate {@IBOutlet弱VAR文本框:的UITextField!
让autocompleteTableView =的UITableView(帧:CGRectMake(0,80,320,120),风格:UITableViewStyle.Plain)VAR pastUrls = [男性,女性,猫,狗,儿童]
VAR autocompleteUrls = [字符串]()覆盖FUNC viewDidLoad中(){
    super.viewDidLoad()    autocompleteTableView.delegate =自
    autocompleteTableView.dataSource =自
    autocompleteTableView.scrollEnabled =真
    autocompleteTableView.hidden =真
}FUNC文本框(文本框:的UITextField!shouldChangeCharactersInRange范围:NSRange,replacementString字符串:字符串!) - GT;布尔
{
    autocompleteTableView.hidden = FALSE
    VAR子=(为的TextField.text NSString的).stringByReplacingCharactersInRange(范围,withString:字符串)    sea​​rchAutocompleteEntriesWithSubstring(子)
    返回true //不知道这一点 - 可能是假的
}FUNC searchAutocompleteEntriesWithSubstring(子:字符串)
{
    autocompleteUrls.removeAll(keepCapacity:FALSE)
    VAR indexOfPastUrls = 0    在pastUrls curString
    {
        让substringRange = curString.rangeOfString(curString)        如果(indexOfPastUrls == 0)
        {
            autocompleteUrls.append(curString)
        }
        indexOfPastUrls = indexOfPastUrls + 1
    }
    autocompleteTableView.reloadData()
}覆盖FUNC didReceiveMemoryWarning(){
    super.didReceiveMemoryWarning()
}FUNC的tableView(的tableView:UITableView的,numberOfRowsInSection段中:int) - GT; INT {
    返回autocompleteUrls.count
}FUNC的tableView(的tableView:UITableView的,的cellForRowAtIndexPath indexPath:NSIndexPath) - GT; {的UITableViewCell    让autoCompleteRowIdentifier =AutoCompleteRowIdentifier
    VAR细胞:=的UITableViewCell tableView.dequeueReusableCellWithIdentifier(autoCompleteRowIdentifier,forIndexPath:indexPath)作为一个UITableViewCell
    让指数=​​ indexPath.row为int    cell.textLabel.text = autocompleteUrls [指数]
    回报细胞
}FUNC的tableView(的tableView:UITableView的,didSelectRowAtIndexPath方法indexPath:NSIndexPath){
    让selectedCell:=的UITableViewCell tableView.cellForRowAtIndexPath(indexPath)!
    的TextField.text = selectedCell.textLabel.text
}
}


解决方案

替换您的 searchAutocompleteEntriesWithSubstring 函数的内容与下面的之一。我希望这会帮助你。

  FUNC searchAutocompleteEntriesWithSubstring(子:字符串)
{
    autocompleteUrls.removeAll(keepCapacity:FALSE)    在pastUrls curString
    {
        VAR的myString:NSString的! = curString作为的NSString        VAR substringRange:NSRange! = myString.rangeOfString(子)        如果(substringRange.location == 0)
        {
            autocompleteUrls.append(curString)
        }
    }    autocompleteTableView.reloadData()
}

I am trying to implement autocompletion, but can't find an example that works in Swift. Below, I'm tring to convert Ray Wenderlich's autocompletion tutorial and example code from 2010. Finally, the code compiles, but the table containing possible completions does not appear, and I don't have the experience to see why it is not unhidden by shouldChangeCharactersInRange.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

@IBOutlet weak var textField: UITextField!
let 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 as NSString).stringByReplacingCharactersInRange(range, withString: string)

    searchAutocompleteEntriesWithSubstring(substring)
    return true     // not sure about this - could be false
}

func searchAutocompleteEntriesWithSubstring(substring: String)
{
    autocompleteUrls.removeAll(keepCapacity: false)
    var indexOfPastUrls = 0

    for curString in pastUrls
    {
        let 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 {
    return autocompleteUrls.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let autoCompleteRowIdentifier = "AutoCompleteRowIdentifier"
    var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier(autoCompleteRowIdentifier, forIndexPath: indexPath) as UITableViewCell
    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        
}
}

解决方案

Replace your searchAutocompleteEntriesWithSubstring function content with the one below. I hope it would help you.

func searchAutocompleteEntriesWithSubstring(substring: String)
{
    autocompleteUrls.removeAll(keepCapacity: false)

    for curString in pastUrls
    {
        var myString:NSString! = curString as NSString

        var substringRange :NSRange! = myString.rangeOfString(substring)

        if (substringRange.location  == 0)
        {
            autocompleteUrls.append(curString)
        }
    }

    autocompleteTableView.reloadData()
}

这篇关于获取自动完成在SWIFT工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆