Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWIFT TableView not loading text

I am making a tableView programatically. For some reason the table view is always empty. Please help me find the bug. Here is my code

// Set all view we will 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's 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!
}
like image 541
pokemon999 Avatar asked Dec 19 '25 19:12

pokemon999


1 Answers

Try this :

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

then replace your function by copying this two functions by command + click on UITableViewDataSource:

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

}

and other one is :

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

}  

and put all the code into this two function and try again becouse in your code this two functions are not getting called but by doing this your function will called and you have put all the required code into this function.

and also remove comment from

self.tableView!.dataSource = self

May be 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!
    }
like image 159
Dharmesh Kheni Avatar answered Dec 21 '25 09:12

Dharmesh Kheni