2

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?

Vlad Alexeev
  • 2,072
  • 6
  • 29
  • 59

6 Answers6

3

If you are using storyboard then remove this line from viewdidload function

self.tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

It worked for me.

Shahzaib Maqbool
  • 1,479
  • 16
  • 25
2

Add Two line inside viewDidLoad() function

self.yourtableviewname.delegate = self
self.yourtableviewname.dataSource = self
iParesh
  • 2,338
  • 1
  • 18
  • 30
1

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

Community
  • 1
  • 1
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

It is happening due to your cell is always create a new instance because it is not coming in if part. So check if your cell is nil then create a instance of your cell like this..

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

    var cell: MyCell! = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier")

    if cell == nil {
                tableView.registerNib(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "reuseIdentifier")
               cell =tableView.dequeueReusableCellWithIdentifier(identifier) as? MyCell
            }
    return cell
  }
}

Here MyCell is the custom cell.

Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
1

You need to self tableView Delegate and Datasource either in viewDidLoad or from Stroyboard.

override func viewDidLoad() {
  super.viewDidLoad()

  tableViewName.delegate = self
  tableViewName.dataSource = self
}
0

You can check whether you forgot to do anything from below two steps

1)Cross Check whether you have connected Datasource, Delegate and IBoutlet For tableview ?

2)Check whether you have set identifier in Storyboard is same as reuseIdentifier ?

Bhavik
  • 209
  • 2
  • 4