mattgabor
mattgabor

Reputation: 2084

UITableView cells not updating in Swift

I'm trying to set the text of a cell in a UITableView inside of a UIViewController and the text is not being updated. Also, nothing gets printed even though I have two print statements, so it seems like the functions are not being called. Here is my code:

class HomePageViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate {
@IBOutlet weak var flaggedTable: UITableView!
@IBOutlet weak var recentTable: UITableView!

var flaggedSerials = [DataCell]()
var recentlyViewedSerials = [DataCell]()

override func viewDidLoad() {
    super.viewDidLoad()

    addSerial("FCGPR0TUG07P", status: "Shipped", issues: 6, reworks: 2, index: 0, flagged: false)    
}    

func addSerial(serial: String, status: String, issues: Int, reworks: Int, index: Int, flagged: Bool)
{
    var currentSerial = DataCell()

    currentSerial.serial = serial
    currentSerial.status = status
    currentSerial.issues = "\(issues) Reported Issues"
    currentSerial.reworks = "\(reworks) Reported Reworks"
    currentSerial.index = index

    if flagged == true {
        flaggedSerials.append(currentSerial)
    }
    else {
        recentlyViewedSerials.append(currentSerial)
    }
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        println(recentlyViewedSerials.count) // This line does not get printed
        return recentlyViewedSerials.count
    }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("FlaggedCell", forIndexPath: indexPath) as! UITableViewCell
    addSerial("FCGPR0TUG07P", status: "Shipped", issues: 6, reworks: 2, index: 0, flagged: false)
    println(recentlyViewedSerials[indexPath.row].serial) // This line does not get printed
    cell.textLabel?.text = recentlyViewedSerials[indexPath.row].serial

    return cell
    }
}

Upvotes: 1

Views: 1897

Answers (3)

KleMiX
KleMiX

Reputation: 332

If func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int isn't called at all, it seems you forgot to assign tableView.dataSource = self or do it in interface builder. And since you seem to have 2 table views in your controller you need to set both of their data sources to self, like:

flaggedTable.dataSource = self
recentTable.dataSource = self

Dont't forget to adapt UITableViewDataSource like:

class HomePageViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {

so full code would look like:

class HomePageViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {

...

override func viewDidLoad() {
    super.viewDidLoad()

    flaggedTable.dataSource = self
    recentTable.dataSource = self
    flaggedTable.delegate = self
    recentTable.delegate = self

    addSerial("FCGPR0TUG07P", status: "Shipped", issues: 6, reworks: 2, index: 0, flagged: false)    
}

Upvotes: 1

asifmohd
asifmohd

Reputation: 999

Replace the class definition line with this line:

class HomePageViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {

Also remember to set the delegate and datasource properties of the tableView to self.

You can do this using interface builder or using code in the viewDidLoad method:

override func viewDidLoad() {
super.viewDidLoad()

// do this for all your tableViews and make sure to have an outlet configured first
tableView.delegate = self
tableView.datasource = self
addSerial("FCGPR0TUG07P", status: "Shipped", issues: 6, reworks: 2, index: 0, flagged: false)    
}   

Upvotes: 5

pbush25
pbush25

Reputation: 5248

Since you're using this UITableView inside of a UIViewController you'll have ot make sure to remember to set the TableView dataSource and delegate in that UIViewController

Edit: Sorry, just saw that @c_rath mentioned the same thing.

Upvotes: 1

Related Questions