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 ...
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 } }
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).
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()
}
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.