Xcode 使用swift在tableviewcell内创建tableview
我用xib文件创建tableview及其内部的tableview单元格 现在我想在xib文件中创建tableview,主要问题是无法在其中创建单元格来定义此单元格的标识符 我一直在犯这个错误 '无法将标识符为AutoCompleteRowIdentifier的单元格出列- 必须为标识符注册nib或类,或连接 故事板中的原型单元 这是我的密码Xcode 使用swift在tableviewcell内创建tableview,xcode,swift,swift2,Xcode,Swift,Swift2,我用xib文件创建tableview及其内部的tableview单元格 现在我想在xib文件中创建tableview,主要问题是无法在其中创建单元格来定义此单元格的标识符 我一直在犯这个错误 '无法将标识符为AutoCompleteRowIdentifier的单元格出列- 必须为标识符注册nib或类,或连接 故事板中的原型单元 这是我的密码 import UIKit class TableViewCell2: UITableViewCell, UITableViewDelegate, UITa
import UIKit
class TableViewCell2: UITableViewCell, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var autocompleteTableView: UITableView!
var pastUrls = ["Men", "Women", "Cats", "Dogs", "Children"]
var autocompleteUrls = [String]()
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
autocompleteTableView.hidden = false
let 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)
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"
let 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
}
override func awakeFromNib() {
super.awakeFromNib()
textField.delegate = self
autocompleteTableView.delegate = self
autocompleteTableView.dataSource = self
autocompleteTableView.scrollEnabled = true
autocompleteTableView.hidden = true
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}}
正如您所知,不能在xib文件中定义标识符
谢谢您需要在
自动完成表格视图
中注册单元格,然后才能将其出列。按如下方式修改代码:
override func awakeFromNib() {
super.awakeFromNib()
textField.delegate = self
autocompleteTableView.delegate = self
autocompleteTableView.dataSource = self
autocompleteTableView.scrollEnabled = true
autocompleteTableView.hidden = true
// Register cell
autocompleteTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "AutoCompleteRowIdentifier")
}
当然有;),你救了我!