user2976267 user2976267 - 4 months ago 7
Swift Question

custom UITableViewCell - not showing

I'm new to Swift.
I try to make a custom looking TableView and added a prototype cell in StoryBoard

enter image description here

I made custom class which I added in the inspector for the cell
enter image description here

Here it's code :

class SearchTableViewCell: UITableViewCell {

@IBOutlet var routeDescriptionLbl: UILabel!
@IBOutlet var ivRoute: UIImageView!
@IBOutlet var distanceLbl: UILabel!
@IBOutlet var routeNumberLbl: UILabel!


override func awakeFromNib() {
//still empty yet
super.awakeFromNib()

}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}
}


Now i made an identifier :
enter image description here

And here's the View Controller for the whole view (partially):

class SearchViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

var transportData : [RouteModel] = []

override func viewDidLoad() {
super.viewDidLoad()
fillMockup()
self.lvMain.reloadData() //in case it's needed after mockup is filled

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewDidAppear(animated: Bool) {

NSLog("viewDidAppear")
}

func fillMockup(){
//just some mockup data to test if it's working
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "44", description: "Ярославль - Сан Тропе",dist: "200 m"))
transportData.append(RouteModel(type: RouteModel.TYPE_TRAM,routeNumber: "11", description: "Ярославль - Сан Тропе",dist: "400 m"))
transportData.append(RouteModel(type: RouteModel.TYPE_R_BUS,routeNumber: "1", description: "Ярославль Главный - Жопа Мира",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_TROLLEY,routeNumber: "24", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "43", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "43а", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "85б", description: "Ярославль - Брагино",dist: "1.4 км"))
transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "38", description: "Ярославль - Брагино",dist: "2.4 км"))
}

public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
NSLog("transportData.count = \(transportData.count)")
return transportData.count
}

public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier") as? SearchTableViewCell
if let cell = cell {
cell.routeNumberLbl.text = transportData[indexPath.row].routeNumber
cell.routeDescriptionLbl.text = transportData[indexPath.row].description
cell.distanceLbl.text = transportData[indexPath.row].dist

return cell
}

return UITableViewCell()

}

func tableView(tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath){
NSLog("You selected cell #\(indexPath.row)!")
let position = indexPath.row;
tableView.deselectRowAtIndexPath(indexPath, animated: false)
}
}


Now, not any Log from table view is being called. An of course, TableView is not shown

What's the problem?

Answer

do like

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

    let cell: SearchTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("reuseIdentifier") as! SearchTableViewCell

     cell.routeNumberLbl.text = transportData[indexPath.row].routeNumber
        cell.routeDescriptionLbl.text = transportData[indexPath.row].description
        cell.distanceLbl.text = transportData[indexPath.row].dist

    return cell
}

updated answer

override func viewDidLoad() {
                super.viewDidLoad()
          self.lvMain.delegate = self // invoke data source and delegate on current class
         self.lvMain.dataSource = self
        fillMockup()



    }

  func fillMockup(){
//just some mockup data to test if it's working
        transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "44", description: "Ярославль - Сан Тропе",dist: "200 m"))
        transportData.append(RouteModel(type: RouteModel.TYPE_TRAM,routeNumber: "11", description: "Ярославль - Сан Тропе",dist: "400 m"))
        transportData.append(RouteModel(type: RouteModel.TYPE_R_BUS,routeNumber: "1", description: "Ярославль Главный - Жопа Мира",dist: "1.4 км"))
        transportData.append(RouteModel(type: RouteModel.TYPE_TROLLEY,routeNumber: "24", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
        transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "43", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
        transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "43а", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
        transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "85б", description: "Ярославль - Брагино",dist: "1.4 км"))
        transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "38", description: "Ярославль - Брагино",dist: "2.4 км"))

      self.lvMain.reloadData() //reload the table when does loaded perfectly 
    }


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

    let cell: SearchTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("reuseIdentifier") as! SearchTableViewCell

     cell.routeNumberLbl.text = transportData[indexPath.row].routeNumber
        cell.routeDescriptionLbl.text = transportData[indexPath.row].description
        cell.distanceLbl.text = transportData[indexPath.row].dist

    return cell
}

for additional example see this