1
votes

I have been trying to add a tableview in my ViewController unfortunately no data is shown in my table view. numberOfSectionsInTableView , numberOfRowsInSection , cellForRowAtIndexPath are not even getting called.I tried tableview inside ViewController as there is another controls like label , text field's and TableView underneath .Because of these label's and text fields I am unable to use a TableViewController. what I have done ...

  1. Created a UIViewController and added controls like label , text fields etc.
  2. From Object Library I dragged a TableView into my UIViewController.
  3. To manage my data from server I created a Model

     class Model {
    
    var name : String
    var email : String
    
    init?(name : String , email : String)
    {
        self.name = name
        self.email = email
    } }
    
  4. as I have dynamic data, for the cell I created a swift file under the Subclass UITableViewCell named userinfoCell and connected my labels(two label's inside TableViewCell one for username and another for email) to this class(userinfoCell).

  5. Finally in my UIViewController I have added the following code to populate my Table View.

Inside my class definition I have initialised a global variable like this

var model = [Model]()

I have created a function that adds data to my model

 func loadMembers()
    {

        let member1 = Model(name: "Caprese Salad", email: "[email protected]")!

        let member2 = Model(name: "Capresed", email: "[email protected]")!



        model += [member1, member2]

    }

on my ViewDidLoad I calls this function

 override func viewDidLoad() {
    super.viewDidLoad()

        loadMembers()
    }



override func numberOfSectionsInTableView(tableView: UITableView) -> Int 
    {
       return 1
    }

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return model.count
    }

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

        let cell = tableView.dequeueReusableCellWithIdentifier("Members", forIndexPath: indexPath) as! userinfoCell

        let member = model[indexPath.row]


        cell.MemberName.text = member.name
        cell.MemberEmail.text = member.email


        return cell
    }

But I am getting an Empty table view. What went wrong in my approach

how can I implement a dynamic UITableView inside UIViewController ???

3
have you set datasource and delegates of tableview? - ios developer

3 Answers

1
votes

Modify your loadMembers method like this,

func loadMembers()
{
    let member1 = Model(name: "Caprese Salad", email: "[email protected]")!

    let member2 = Model(name: "Capresed", email: "[email protected]")!

    model += [member1, member2]

    // reload the table view after modifying the data source.
    self.tableView.reloadData()   
}
0
votes

Ctrl drag from table view to View controller and set datasource and delegate

enter image description here

then call self.tableView.reloadData() in loadMember()

0
votes

Hold control and drag from tableview to view controller and set data source and delegate like this...

enter image description here