SWIFT TableView does not load text

I am making tableView programmatically. For some reason, the table view is always empty. Please help me find the error. Here is my code

// Set up all the views we need

func setupTableView() {
    self.tableView = UITableView(frame: CGRectMake(0, 20, 320, self.heighTableView))
    self.tableView!.tableHeaderView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, CGFloat(self.heighTableViewHeader)))
    self.tableView!.backgroundColor = UIColor.clearColor()

self.tapMapViewGesture = UITapGestureRecognizer(target: self, action: "handleTapMapView:")
        self.tapTableViewGesture = UITapGestureRecognizer(target: self, action: "handleTapTableView:")
        self.tapTableViewGesture!.delegate = self
        self.tableView!.tableHeaderView?.addGestureRecognizer(self.tapMapViewGesture!)
        self.tableView!.addGestureRecognizer(self.tapTableViewGesture!)

        // Init selt as default tableview delegate & datasource
        //self.tableView!.dataSource = self
        self.tableView!.delegate = self
        self.view.addSubview(self.tableView!)
    }


// UITableViewDataSource Methods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat {

    return 80

}


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

    var identifier = "Cell"

       var cell = tableView.dequeueReusableCellWithIdentifier(identifier) as UITableViewCell?

        cell!.textLabel!.text = "Hello World !"
   // }
 return cell!
}

      

+3


source to share


1 answer


Try the following:

class ViewController: UIViewController, UITableViewDelegate, MKMapViewDelegate, UIGestureRecognizerDelegate,UITableViewDataSource

      

then replace your function by copying those two functions with + command by clicking on UITableViewDataSource

:

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

}

      

and the other:

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

}  

      



and put all your code in that two functions and try again, because in your code, these two functions are not called, but this is how your function is called, and you put all the code you need in that function.

and also remove comment from

self.tableView!.dataSource = self

Maybe this can help you.

Edit for this answer:

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

    var identifier : NSString = "Cell"

    var cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? UITableViewCell

    if !(cell != nil) {
        cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: identifier)
    }
    cell?.textLabel?.text = "Hello World"
    return cell!
    }

      

+3


source







All Articles