Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create tableview inside tableviewcell using swift

well i create tableview and inside of it tableview cell with xib file

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

i keep get this error

'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'

this is my code

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

as you know you can't define identifier inside xib file

Image for the tableview cell

thanks

like image 541
Ahmed Safadi Avatar asked Feb 06 '26 18:02

Ahmed Safadi


1 Answers

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")
}
like image 58
Rafa de King Avatar answered Feb 09 '26 11:02

Rafa de King



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!