UITableViewCells not showing the first time

I am trying to create autostart using iOS 8, Swift and Xcode 6.3

I have a problem that I am trying to solve but I gave up ... Hope someone can help here. The problem is that (custom) UITableViewCell

doesn't show up when the initial is dataSource

empty. When adding data in dataSource

and reloading the tableView

cells SHOULD be displayed, but they are not ... At least the first time they are not ... The second time they do ... When I initialize the table with non-empty data, the problem does not occur. I think something went wrong with dequeueReusableCellWithIdentifier

. No reusable cells or anything else found at the beginning. But I do not know why...

Relevant code, in ViewController.swift:

// filteredWords is a [String] with zero or more items

@IBAction func editingChanged(sender: UITextField) {
    autocompleteTableView.hidden = sender.text.isEmpty
    filteredWords = dataManager.getFilteredWords(sender.text)
    refreshUI()
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! AutocompleteTableViewCell
    cell.title.text = filteredWords[indexPath.row]
    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return filteredWords.count
}

func refreshUI() {
    self.autocompleteTableView.reloadData()
}

      

I created a sample project on github:

https://github.com/dirkpostma/swift-autocomplete

And a movie on YoutTube to show you what goes wrong:

https://www.youtube.com/watch?v=ByMsy4AaHYI

Can anyone look at it and spot the error ??

Thanks in advance!

+3


source to share


2 answers


You accidentally hid your camera.

  • Open Main.storyboard
  • Select cell
  • Uncheck Hidden


Side note: As for why it displays a second time with the cell hidden? This seems to be a bug. It should still be hidden (print cell.hidden, note that this is always true, despite displaying text on the screen).

+3


source


I think you need to change your code. Take a look at below code. This is because if you remember in Objective C, you had to check if the value was Cell

zero and then initialize it. A reuse id usually reuses an already created cell, but on first run it doesn't work because there is no cell to use. Your current code assumes that the cell is being created (reused) because you are using! in the declaration, so if you use an optional (?) it can be null and then you can create a cell



    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? AutocompleteTableViewCell

    if cell == nil 
    {
        //You should replace this with your initialisation of custom cell
        cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL") 

    }

    cell.title.text = filteredWords[indexPath.row]
    return cell

      

+1


source







All Articles