我正在使用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)
}
答案 0 :(得分:1)
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 。