简体   繁体   中英

Swift dynamic tableview in UIViewController

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: "caprreswe@gmail.com")!

        let member2 = Model(name: "Capresed", email: "pepperoni@gmail.com")!



        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 ???

Modify your loadMembers method like this,

func loadMembers()
{
    let member1 = Model(name: "Caprese Salad", email: "caprreswe@gmail.com")!

    let member2 = Model(name: "Capresed", email: "pepperoni@gmail.com")!

    model += [member1, member2]

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

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

在此处输入图片说明

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

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM