gpt4 book ai didi

ios - 使用自动完成文本字段时不调用 cellForRowAtIndexPath 方法

转载 作者:行者123 更新时间:2023-11-29 01:56:47 24 4
gpt4 key购买 nike

我正在 Swift 中开发自动完成文本字段。

我使用了这个例子:

Getting autocomplete to work in swift

但对我来说 cellForRowAtIndexPath 方法根本没有调用。我不知道我在这里犯了什么错误。

我的代码是这样的:

 @IBOutlet weak var autocompleteTableView: UITableView!
@IBOutlet weak var searclLocationTextField: UITextField!

var pastUrls = ["Men", "Women", "Cats", "Dogs", "Children"]
var autocompleteUrls = [String]()


override func viewDidLoad() {
super.viewDidLoad()


autocompleteTableView.scrollEnabled = true
autocompleteTableView.hidden = true

// Do any additional setup after loading the view.
}

// 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 textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
println("banana")
autocompleteTableView.hidden = false
var substring = (searclLocationTextField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)

searchAutocompleteEntriesWithSubstring(substring)
autocompleteTableView.reloadData()
return true
}


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()
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return autocompleteUrls.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let autoCompleteRowIdentifier = "AutoCompleteRowIdentifier"
var cell = tableView.dequeueReusableCellWithIdentifier(autoCompleteRowIdentifier) as? UITableViewCell

if let tempo1 = cell
{
let index = indexPath.row as Int
cell!.textLabel!.text = autocompleteUrls[index]
} else
{
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: autoCompleteRowIdentifier)
}
return cell!
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedCell : UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
searclLocationTextField.text = selectedCell.textLabel!.text

println(searclLocationTextField.text)
}

最佳答案

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()
}

在重新加载 tableView 之后,您最终会在这一行 autocompleteUrls.append(curString) 获得数组,因此将数组计数记录在 autocompleteUrls.append(curString) 这一行下面,如果您肯定会得到一些东西, cellForRowAtIndexPath 将被命中.

关于ios - 使用自动完成文本字段时不调用 cellForRowAtIndexPath 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30754555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com