Reputation: 584
I want to make a tableview with different people. You can add a person by clicking the add button. When you click the add button, you fill out the information 'first name','last name',and 'date'. When you click the done button, it goes back to the tableview and the cell shows the persons first name.
I created a new swift file that has a struct:
struct person {
var firstName : String
var lastName : String
var date : String
}
In the tabledetailviewcontroller.swift (controller where you add a person) I declare IBOutlets for each field and set each value:
@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var lastName: UITextField!
@IBOutlet weak var dateText: UITextField!
let first:String = ""
let last:String = ""
let date = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .MediumStyle, timeStyle: .ShortStyle)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "doneSegue" {
first = firstName.text
last = lastName.text
date = dateText.text
}
}
In the tableviewcontroller.swift I have declared an array of people with type 'person':
var people = [person]()
and a function that returns the number of people in the array.
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return people.count
}
then I have a function that sets the text value of the cell override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("issueCell", forIndexPath: indexPath) as UITableViewCell
//cell.textLabel.text = issues.dateTime[indexPath.row] //Fixed this to this =>
cell.textLabel.text = (issues[indexPath.row]).dateTime
return cell
}
finally I have a done button that appends the new person to the array (this part keeps giving me errors...)
@IBAction func done(segue:UIStoryboardSegue){
var addPersonVC = segue.sourceViewController as AddPersonViewController
let newPerson = person(date: (addPersonVC.date)) //Breaks here for no reason!
people.append(newPerson)
}
My question is, how can I make each cell store multiple fields (first, last, date), save it in under each person and have the person's name show up in the cell in the tableview? I want to be able to edit a cell and change information about that person.
I am very new to swift :/ I greatly appreciate it!
EDIT: I have fixed a few things, now it breaks at the done function!
Upvotes: 0
Views: 1143
Reputation: 4641
If I am not wrong or missing some code, you have two errors:
Upvotes: 1