0

I have a UITableView subview that should display Game Center names, but does not update when reloadData() is called.

The code initiating the tableView and the reloadData function are as follows:

import UIKit
import QuartzCore
import GameKit

class LobbyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var lobbyView: UITableView!
var lobbyTimer = NSTimer()
var bStartOfTimer: Bool = false
var timerTextView: UITextView = UITextView()


//TABLE VIEW
func initTableView() {
    lobbyView = UITableView(frame: CGRectMake(20, 150, self.view.frame.width - 40, 2*self.view.frame.height/3), style: UITableViewStyle.Plain)
    lobbyView.layer.cornerRadius = 10
    lobbyView.registerClass(LobbyViewCell.self, forCellReuseIdentifier: "cell")

    //lobbyView.frame = CGRectMake(20, self.view.frame.height/3, self.view.frame.width - 40, self.view.frame.height/3)

    lobbyView.delegate = self
    lobbyView.dataSource = self
}

override func viewDidLoad() {

    super.viewDidLoad()

    var background = UIView(frame: self.view.frame)
    background.backgroundColor = UIColor(netHex: cs_navy)
    self.view.addSubview(background)

    addText()
    lobbyTimer = NSTimer.scheduledTimerWithTimeInterval(0.02, target: self, selector: Selector("updateTimer"), userInfo: nil, repeats: true)
    initTableView()

    self.view.addSubview(lobbyView)
}

    func update(){
    if (lobbyView != nil) {
        self.lobbyView.reloadData()
    }
}

I have all the other necessary functions, and reloadData() was working until I changed the tableView to a subview. Could the dataSource be incorrect?

1
  • where u called the function name update Jul 28, 2015 at 6:06

1 Answer 1

0

I'm not sure what was the previous case but I'm assuming that your controller was a UITableViewController. In that case, the main view of the controller was a UITableView. However when you switch to a UIViewController and add a UITableView programmatically, you need to make sure your UITableView is in the front in your view hierarchy. Try using

self.view.insertSubview(lobbyView, 0)

instead of

self.view.addSubview(lobbyView)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.