Jasper Rosiers Jasper Rosiers - 2 years ago 122
Swift Question

TableView only shows content when clicked

I'm making a TableView to show the shops in our town, along with their category. I have a problem with showing the shops in the table view: first off, the first shop doesn't show, and second, the shops only become visible when clicked upon. You can find a video of the problem here

The code of the table view:

import UIKit

class MasterTableViewController: UITableViewController {

// MARK: - Properties
var detailViewController: DetailViewController? = nil
var winkels = [Winkel]()

// MARK: - View Setup
override func viewDidLoad() {

winkels = [
Winkel(category:"Literature", name:"Standaard"),
Winkel(category:"Literature", name:"Acco"),
Winkel(category:"Clothing", name:"H&M"),
Winkel(category:"Clothing", name:"C&A"),
Winkel(category:"Clothing", name:"Patio"),
Winkel(category:"Restaurants", name:"De 46"),
Winkel(category:"Restaurants", name:"Het hoekske"),
Winkel(category:"Supermarkets", name:"Carrefour"),
Winkel(category:"Supermarkets", name:"Colruyt")

if let splitViewController = splitViewController {
let controllers = splitViewController.viewControllers
detailViewController = (controllers[controllers.count - 1] as! UINavigationController).topViewController as? DetailViewController

override func viewWillAppear(animated: Bool) {
clearsSelectionOnViewWillAppear = splitViewController!.collapsed

override func didReceiveMemoryWarning() {

// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

let winkel = winkels[indexPath.row]
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
cell.textLabel!.text = winkel.name
cell.detailTextLabel?.text = winkel.category
return cell

// MARK: - Segues
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let winkel = winkels[indexPath.row]
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailWinkel = winkel
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true


Does anyone have an idea on how to fix these problems? Thanks in advance!

Answer Source

Remove the line
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell") cause you already have
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download