Reputation: 1861
I'm creating a sort of Contact list app w/ Xcode and everything was running smoothly until it came time for the app the actually add the new contact to the Contact list. What happens is that Once the user goes to the ViewController that helps the user create a new contact; the previously added Contact is removed from the list. I've tried to investigate and what I've discovered is that once I create a new Element (or Contact)for the Array; my code for some reason deletes the old Contact and replaces it with the newly created one. Whats also interesting is that by simply going to another page on my app (going to a new ViewController) my app for some reason removes the newly created Contact from the Contact list and leaves my TableView empty.
Here is a breakdown of my Coding and what I've tried:
this is the initial ViewController (I've implemented a TableView and created arrays that are organized in a cell within the TableView)
class ViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {
@IBOutlet var TableView: UITableView!
var names = [String]()
var numbers = [String]()
override func viewDidAppear(animated: Bool) {
TableView.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
cell.Name.text = names[indexPath.row]
cell.PhoneNumber.text = numbers[indexPath.row]
cell.reloadInputViews()
return cell
}
}
This is Where my user will create a new Contact and how I try to append the new Contact to the contact list
classPickViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if Save.touchInside{
var VC :ViewController = segue.destinationViewController as! ViewController
if VC.names.count >= 1 {
VC.names.insert(NameText, atIndex: (VC.names.count)+1)
VC.numbers.insert(PhoneText,atIndex: (VC.numbers.count)+1)
}
else{
VC.names.insert(NameText, atIndex: (VC.names.count)+0)
VC.numbers.insert(PhoneText,atIndex: (VC.numbers.count)+0)
}
}
}
@IBAction func SaveContact(sender: UIButton) {
performSegueWithIdentifier("SaveEm", sender: self)//this takes the usr to the Contact list
}
Here is the code Ive used to initialize my Cells in the TableView (UITableViewCell file)
class CustomCell: UITableViewCell {
@IBOutlet var Name: UILabel!
@IBOutlet var PhoneNumber: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Upvotes: 0
Views: 253
Reputation: 114846
Your app is navigating from ViewController->classPickViewController->ViewController, so you have a new instance of ViewController
which will start with a new empty array.
You should look into unwind segues so that you can move back to the presenting ViewController
.
You also need to use something like Core Data to persist your data between executions of your app.
I would store an array of a "Contact" struct rather than having two arrays
Upvotes: 1
Reputation: 395
reloadInputViews() is an overkill when you are already in the cellForRowAtIndexPath. remove that and put reloaddata appropriately. If needed put again when you know that the control flow is over. So remove reloadInputViews(), and revisit each view of tableviewcell in cellForRowAtIndexPath instead.
Upvotes: 0