First of all, I am a beginner! So it's a bit complicated, but basically I am trying to create a separate view controller that displays information held in a struct/string
with objects. I am making a directory. I have two controllers, one for the tableView
(called DirectoryTableViewController
) and one for the detail view (called FacultyViewController
) and then I have a swift file (called People) that has manages the String. I am eventually going to add name, phone, email and an image to the String
, but for now I am just doing the names.
My problem is that it is working and I need some pointers. Thanks!!
Here is my DirectoryTableView:
import UIKit
struct peoples {
var teacherString: String!
var image: UIImage!
}
class DirectoryTableViewController: UITableViewController {
var detailViewController: DetailViewController? = nil
var array : [People]!
override func viewDidLoad() {
super.viewDidLoad()
print(array)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("directoryCell", forIndexPath: indexPath)
let person = array[indexPath.row]
cell.textLabel!.text = person.teacherString
return cell
}
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
}
Here is my NewViewController:
import UIKit
class NewViewController: UIViewController {
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var phoneTextField: UITextField!
@IBOutlet weak var emailTextTield: UITextField!
var array : [People] = []
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func crateObjectButton(sender: AnyObject) {
let object = People(name: nameTextField.text! , phone: phoneTextField.text!, email: emailTextTield.text!)
array.append(object)
performSegueWithIdentifier("TeacherData", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "TeacherData" {
let dvc = segue.destinationViewController as? DirectoryTableViewController
dvc!.array = array
}
}
}
Here is People.swift (model):
import Foundation
class People {
var name : String
var phone: String
var email: String
init(name: String, phone: String, email: String) {
self.name = name
self.phone = phone
self.email = email
}
}
Thanks again!
As per your question , you have to make a model
import Foundation
class People {
var name : String
var phone: String
var email: String
init(name: String, phone: String, email: String) {
self.name = name
self.phone = phone
self.email = email
}
}
and make a view controller from where you gather all these details for eg: like this
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var phoneTextField: UITextField!
@IBOutlet weak var ageTextTield: UITextField!
var array : [People] = []
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func crateObjectButton(sender: AnyObject) {
let object = People(name: nameTextField.text! , phone: phoneTextField.text!, email: ageTextTield.text!)
array.append(object)
performSegueWithIdentifier("TeacherData", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "TeacherData" {
let dvc = segue.destinationViewController as? TableviewController
dvc!.array = array
}
}
}
and this segue take you to the tableviewcontroller where in viewdidLoad i am printing the array of teachers.
import UIKit
class TableviewController: UITableViewController {
var array : [People]!
override func viewDidLoad() {
super.viewDidLoad()
print(array)
}
}
and you go to the deatil view controller of a selected teacher by using this method:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
object = array![indexPath.row]
performSegueWithIdentifier("yourdetailviewcontrollersegue", sender: self)
}
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.