使用swift在tableviewcell内创建tableview [英] create tableview inside tableviewcell using swift
问题描述
好吧,我用xib文件创建tableview并在其中创建tableview单元
well i create tableview and inside of it tableview cell with xib file
现在我想在xib文件中创建tableview,主要问题是你不能在其中创建单元格来定义这个单元格的标识符
now i want create tableview inside xib file , the main problem is that u can't create cell inside of it to define the identifier for this cell
我一直收到这个错误
'无法将带有标识符 AutoCompleteRowIdentifier 的单元格出列 -必须为标识符注册一个笔尖或一个类,或者连接一个故事板中的原型单元格'
'unable to dequeue a cell with identifier AutoCompleteRowIdentifier - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
这是我的代码
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 文件中定义标识符
as you know you can't define identifier inside xib file
谢谢
推荐答案
您需要先使用 autocompleteTableView
注册单元格,然后才能将其出列.像这样修改你的代码:
You need to register a cell with autocompleteTableView
before you can dequeue it. Modify your code like this:
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")
}
这篇关于使用swift在tableviewcell内创建tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!