2
我跟着建議這裏添加自動完成裏面的UITableViewCell的UITextField斯威夫特
https://stackoverflow.com/a/32948918/5447089
但似乎在文本字段裏面UIViewController
工作。在我的情況下,TF在UITableViewCell
之內,當有一些數據用於自動完成時,不會顯示含有建議的表格。
另外cellForRowAtIndexPath
沒有被調用。其他代表方法如numberOfRowsInSection
正常工作。
這可能是什麼原因?
import UIKit
import CoreData
class AddElemTableViewCell: UITableViewCell, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var myTextField: UITextField!
var autocompleteTableView = UITableView(frame: CGRectMake(0,80,320,120), style: UITableViewStyle.Plain)
var elements = [「Beer」, 「Bear」, 「Park」, 「Pad」]
var autocompleteElements = [String]()
var currentElem: Elem!
override func awakeFromNib() {
super.awakeFromNib()
myTextField.delegate = self
autocompleteTableView.delegate = self
autocompleteTableView.dataSource = self
autocompleteTableView.scrollEnabled = true
autocompleteTableView.hidden = true
myTextField.addTarget(self, action: #selector(AddElemTableViewCell.didChangeText(_:)), forControlEvents: .EditingChanged)
}
func didChangeText(textField:UITextField) {
autocompleteTableView.hidden = false
let substring = (myTextField.text! as NSString)
searchAutocompleteEntriesWithSubstring(substring as String)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func searchAutocompleteEntriesWithSubstring(substring: String)
{
autocompleteElements.removeAll(keepCapacity: false)
for curString in elements
{
let myString:NSString! = curString as NSString
let substringRange :NSRange! = myString.rangeOfString(substring,options: [.CaseInsensitiveSearch])
if (substringRange.location == 0)
{
autocompleteElements.append(curString)
}
}
autocompleteTableView.reloadData()
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == myTextField {
if (textField.text!.characters.count > 0) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedObjectContext = appDelegate.managedObjectContext
let entityElement = NSEntityDescription.entityForName("element", inManagedObjectContext: managedObjectContext)
let element = element(entity: entityElement!, insertIntoManagedObjectContext: managedObjectContext)
element.name = textField.text!
do {
try managedObjectContext.save()
} catch {
let saveError = error as NSError
print(saveError)
}
textField.text! = ""
textField.placeholder = 「add new element」
self.endEditing(true)
}
}
return true
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return autocompleteElements.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let autoCompleteRowIdentifier = "AutoCompleteRowIdentifier"
let cell = UITableViewCell(style: UITableViewCellStyle.Default , reuseIdentifier: autoCompleteRowIdentifier)
let index = indexPath.row as Int
cell.textLabel!.text = autocompleteElements[index]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedCell : UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
myTextField.text = selectedCell.textLabel!.text
}
}
你真的需要發佈你所有的代碼。有無數的方法來編寫一個編程錯誤。 :-) – BaseZen
@BaseZen我編輯了代碼。 'AddElemTableViewCell'是一個動態原型單元。 – orca