【问题标题】:create tableview inside tableviewcell using swift使用 swift 在 tableviewcell 内创建 tableview
【发布时间】:2016-03-07 19:35:15
【问题描述】:

我用 xib 文件创建 tableview 并在其中创建 tableview 单元格

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

我一直收到这个错误

'无法将具有标识符 AutoCompleteRowIdentifier 的单元格出列 - 必须为标识符注册一个 nib 或一个类或连接一个 故事板中的原型单元格'

这是我的代码

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

谢谢

【问题讨论】:

    标签: xcode swift swift2


    【解决方案1】:

    您需要先向autocompleteTableView 注册一个单元格,然后才能将其出列。像这样修改你的代码:

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

    【讨论】:

    • 确定我做到了;),你救了我!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    相关资源
    最近更新 更多