0
votes

I'm setting up custom cell for a tableview and got it almost work properly except some annoying errors. So I setup a prototype cell in storyboard with identifier: RankingCell and class: RankingCell.

I setup a very basic class file : RankingCell.swift

import Foundation
import UIKit


 class RankingCell: UITableViewCell {
    @IBOutlet var firstName: UILabel!
    @IBOutlet var lastName: UILabel!
}

The outlets are correctly linked to field inside the custom cell. Now here is the view controller file:

class RankingViewController: UIViewController, UITableViewDelegate {

    var testArray = ["Ben", "rob"]
    var testArray2 = ["mel", "duval"]


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

     func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

        let cell = tableView.dequeueReusableCellWithIdentifier("RankingCell") as RankingCell //Error I here

        cell.firstName.text = testArray[indexPath.row]
        cell.lastName.text = testArray2[indexPath.row]

        return cell
    }

    override func viewDidLoad() {
    super.viewDidLoad()
    tableView.reloadData() //Error II here


    }

}

The error I outlined by the complier is "use of undeclared type 'RankingCell'". What's weird is that when I compile, it compiles with no problem, and even displays correct data in the cells. Still I would prefer to do things properly and get ride of the outlined error.

The second error is in viewdidload. I simplified the code in the example, but I pull data from a backend, and once I have it, I need to reload the table so I put there tableView.reloadData() which trigger error: "(UITableView, numberOfRowsInSection: Int) -> Int' does not have a member named 'reloadData'". Thus I can't have the table reload with appropriate data. Maybe you know how to have data reloaded properly from viewdidload ?

Please answer in swift.

1
Are you also setting the numberOfSections in the table? And heightForCellAtIndexPath?Glynbeard
Issue here is with your custom cell creation. See this answer to create cell in proper way - stackoverflow.com/questions/24166662/swift-tableview-for-ios/…Kampai

1 Answers

1
votes

Ok, I finally resolved the issues:

The first error of undeclared type.... I spent hours on that..... and all what was needed, was to restart xcode. I couldn't believe that when I read someone with similar problem. The fact that the error was showing in red in xcode, but that the project could still be build and run without error was suspicious.... also the colors for that part of code in xcode were wrong... anyway something got corrupted and restarting xcode fixed it.

The second error, to reload the table view: I simply created an iboutlet for the tableview, and then called it in viewdidload after work on the backend to retrieve data. Again, there was a corruption where I couldn't ctrl+drag the tableview from stroyboard into the file: I had to type the outlet, and then link it and it worked, not the other way arround. So instead of invoking tableView.reloadData() I invoke userFriendsTableView.reloadData(), userFriendsTableView being the name of the custom outlet, and now it all works.