xcode - 使用swift在tableviewcell中创建tableview

标签 xcode swift swift2

好吧,我用 xib 文件创建了 tableview 和它里面的 tableview 单元格

现在我想在 xib 文件中创建 tableview,主要问题是你无法在其中创建单元格来定义此单元格的标识符

我一直收到这个错误

'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 文件中定义标识符

Image for the tableview cell

谢谢

最佳答案

您需要先使用 autocomplete TableView 注册单元格,然后才能将其出列。像这样修改您的代码:

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

关于xcode - 使用swift在tableviewcell中创建tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35852405/

相关文章:

objective-c - 在 Swift 2 中子类化 AFHTTPSessionManager 的正确方法

ios - Swift - 无法将类型 'UINavigationController' 的值转换为 'MapViewController'

ios - API 未返回包含原始数据的有效响应。但在 postman 身上工作得很好

xcode - 如何首先将新数据加载到 TableView - Swift

objective-c - MKMapView 缩放用户位置和注释

swift - 如何确定在swift中sender func之外选择了哪个分段控件

ios - 从 Swift 函数中的异步调用返回数据

swift - 使用 Swift 将特定键/值对保存到 Firebase 中的用户类

ios - Apple Mach-O 链接器 (ld) 错误 swift 3.0 和 Xcode 8.3.3

swift - 使用防护/if 语句来设置多个 CollectionvViewCell 样式的良好做法?