How to add data to the tableView and reload? - ios

I'm trying to add a cell to a tableView that's in viewController by sending data via a segue from another viewController.
class FavoritesViewController: UIViewController {
var shops = [
"hello world",
"hello world",
"hello world"
]
#IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
table.delegate = self
table.dataSource = self
table.reloadData()
}
}
//protocol FavoritesDelegate: class {
// func add(_ shopName: String)
//}
extension FavoritesViewController: UITableViewDelegate, UITableViewDataSource{
func add(_ shopName: String) {
print(shopName)
shops.append(shopName)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return shops.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
cell.textLabel?.text = shops[indexPath.row]
return cell
}
// define the action. In this case "delete"
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
// do the actual deleting
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
tableView.beginUpdates()
shops.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
}
}
}
And here's the function call in the other viewController (prepare):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
favoritesDestinationVC = segue.destination as! FavoritesViewController
favoritesDestinationVC.add(shopName!)
}
I know what's causing the error (favoritesDestinationVC creates a new instance where tableView is nil), but I don't know how to solve it. Any ideas on how I could add an entry to the tableView that way (and updating the table afterwards) without my app crashing?

Make your shops var public and then in the segue prepare callback use it directly. Check a variable first whether it has a valid value other than the nil, if it does then proceed to avoid crashes. You can do it using an if statement to verify as you can see in the sample code. Or you can use optionals to avoid crashes. See the code below.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is FavoritesViewController {
let destinationVC = segue.destination as? FavoritesViewController // Use optionals which has ? (question mark)
// To avoid crashes check if shopName has a valid value other than nil
if let newShopName = shopName {
// It is possible that the "shopName" has a nil value when the program reaches here.
// That's why we will use the verified "newShopName" instead of "shopName" itself to avoid any crash.
destinationVC?.shops.append(newShopName) // What I recommend, if not convenient for you just delete this line,
// destinationVC?.add(newShopName) // after deleting the recommended line, uncomment this line for your convenience.
}
}
}

Related

Swift UITableView reloadData() method unexpectedly found nil error

I'm trying to create a very simple todo list app in swift and when I call the reloadData method on my UITableView I get this error: "Unexpectedly found nil while implicitly unwrapping an Optional value". I'm calling this method when the user clicks an add button after typing something into a text field on a separate view controller from the tableView. The thing they type is supposed to get added to the table view, but it doesn't, and I just get an error.
I looked online and found people with similar problems but I couldn't figure out how to implement them into my code or didn't understand them as I am very new to swift. I also tried putting the text field on the same view controller as the table view and that fixed the problem, so I'm guessing it has something to do with that.
I have all my code in ViewController.swift. Here it is:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var editButton: UIBarButtonItem!
#IBOutlet weak var textField: UITextField!
var tableViewData = ["Apple", "Banana", "Orange", "Peach", "Pear"]
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: Tableview methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = tableViewData[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// print(tableViewData[indexPath.row])
}
// Allows reordering of cells
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
// Handles reordering of cells
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let item = tableViewData[sourceIndexPath.row]
tableViewData.remove(at: sourceIndexPath.row)
tableViewData.insert(item, at: destinationIndexPath.row)
}
// Allow the user to delete cells
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCell.EditingStyle.delete {
tableViewData.remove(at: indexPath.row)
tableView.reloadData()
}
}
// MARK: IBActions
#IBAction func edit(_ sender: Any) {
tableView.isEditing = tableView.isEditing
switch tableView.isEditing {
case true:
editButton.title = "Done"
case false:
editButton.title = "Edit"
}
}
#IBAction func add(_ sender: Any) {
let item: String = textField.text!
tableViewData.append(item)
textField.text = ""
tableView.reloadData() // <------ **This line gives me the error**
}
}
Also, I tried optional chaining on the line that gave me an error by writing, tableView?.reloadData(). It makes the error go away, but none of the items get added to the table view.
Not sure if it's necessary, but here is an image of the storyboard so you can see all the screens
Sorry if this is a really obvious problem. Like I said I'm very new to swift and iOS applications in general.
Thanks in advance!
It looks like you are assigning ViewController class to both your first controller (which holds the table view) AND to your second controller (with the text field).
That's not going to work.
Add this class to your project, assign it as the "New Item" view controller's Custom Class, and connect the #IBOutlet and #IBAction:
class NewItemViewController: UIViewController {
// callback closure to tell the VC holding the table view
// that the Add button was tapped, and to
// "send back" the new text
var callback: ((String) -> ())?
#IBOutlet weak var textField: UITextField!
#IBAction func add(_ sender: Any) {
let item: String = textField.text!
callback?(item)
textField.text = ""
}
}
Next, change your ViewController class to the following:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var editButton: UIBarButtonItem!
var tableViewData = ["Apple", "Banana", "Orange", "Peach", "Pear"]
override func viewDidLoad() {
super.viewDidLoad()
// if you're not already seeing "Apple", "Banana", "Orange", "Peach", "Pear"
// add these two lines
//tableView.dataSource = self
//tableView.delegate = self
}
// MARK: Tableview methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableViewData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = tableViewData[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// print(tableViewData[indexPath.row])
}
// Allows reordering of cells
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
// Handles reordering of cells
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let item = tableViewData[sourceIndexPath.row]
tableViewData.remove(at: sourceIndexPath.row)
tableViewData.insert(item, at: destinationIndexPath.row)
}
// Allow the user to delete cells
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCell.EditingStyle.delete {
tableViewData.remove(at: indexPath.row)
tableView.reloadData()
}
}
// MARK: IBActions
#IBAction func edit(_ sender: Any) {
tableView.isEditing = !tableView.isEditing
switch tableView.isEditing {
case true:
editButton.title = "Done"
case false:
editButton.title = "Edit"
}
}
// when "New Item" button is tapped, it will segue to
// NewItemViewController... set the callback closure here
// prepare for segue is called when you have created a segue to another view controller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// error checking is always a good idea
// this properly unwraps the destination controller and confirms it's
// an instance of NewItemViewController
if let vc = segue.destination as? NewItemViewController {
// callback is a property we added to NewItemViewController
// we declared it to return a String
vc.callback = { item in
self.tableViewData.append(item)
self.tableView.reloadData()
self.navigationController?.popViewController(animated: true)
}
}
}
}
When you tap the "Add Item" button, we're assuming you have that connected to segue to the "New Item" view controller. By implementing:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
we will get a reference to the "New Item" view controller that is about to appear, and we'll assign it a "callback closure".
When we type some text and tap the "Add" button in the next controller, it will "call back" to the first controller, passing the newly typed text. That is where we'll update the data array, reload the table, and pop back on the navigation stack.

I want to identify the Array with a String which I got from a View before with Segue

Im a absolute beginner and my English isn't well .
I would like to create a program for a person who wants to learn German.
He can pick up a topic from a picker view and then display the translation in a table view with audio and a picture.
My problem right now is that I can send the topic(SegueStatus1) from the Picker view to my table view( which contains all the arrays), but I can not identify the array with the variable that I had before. I have now manually selected the Array(Aile).
The TABLEVIEW
import UIKit
// Two Arrays which can be chosen
var Aile = ["Anne","Baba","Dede"]
var ABC = ["A","B","C"]
var myIndex = 0
class TableEins: UITableViewController {
override func viewDidLoad() {super.viewDidLoad()}
//Two Strings from View before
var SegueStatus1: String?
var SegueStatus2: String?
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{return Aile.count}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = Aile[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
myIndex = indexPath.row
performSegue(withIdentifier: "AuswendigLernenSegue2", sender: self)
}
}
I am thankful for every help
EDIT :
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if (segue.identifier == "AuswendigLernenSegue")
{
let Status1: TableEins = segue.destination as! TableEins
Status1.SegueStatus1 = KonuT.text
}
I have a pickerView and a tableView both are working.
In the next step I want the pickerView and the tableView being connected.
The variable that determines which array I want to display is SeagueStatus1.
I want to use a solution that gets my variable right where the name of the array is needed.
Hope this helps:
class TableEins: UITableViewController {
// Avoid using magic strings.
let cellIdentifier = "cell"
let segueIdentifier = "AuswendigLernenSegue2"
// Always use lowercases for your variable and make them `let` unless you're going to manipulate them later. This tells everyone it's set only once and never modified.
// Also avoid global variables, I nested these inside the TableEins.
// Use snake case (ex: someVariable) for all variables. This lets someone identify the variable easily from classes and makes your code less confusing.
let aile = ["Anne","Baba","Dede"]
let abc = ["A","B","C"]
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return aile.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
cell.textLabel?.text = aile[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// This will always perform this segue no matter what cell you click on.
// If you want to perform this segue on only some of the cells, you need to check the index paths
performSegue(withIdentifier: segueIdentifier, sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
// Guard is a great way to prevent from having too many nested if statetments and knowing your method execution stops here.
guard segue.identifier == segueIdentifier else { return }
// Get the index path from the sender
guard let indexPath = sender as? IndexPath else { return }
// Get your data using the index path and pass it to your view controller we are navigating to
// You were loading the TableEins as the destination view controller. Thats this class. You may have made a mistake here.
}
}

Each cell open view

How to make Each Cell open the specific view for its indexpath.
In tableview didselect what should i do to make each cell open as its own indexpath so each cell have a different data in the next view
i am tryin when click in a cell in the tableview the next view present it self with it's own data as the next view contain a uitextview like in note app
what should i apply at row didselect
// MARK: -TableFunctions
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return SavingTasks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let newtask = self.SavingTasks[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.TheLabel?.text = newtask
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if(editingStyle == .delete)
{
self.SavingTasks.remove(at: indexPath.row)
self.TasksTable.deleteRows(at: [indexPath], with: .fade)
GetData()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let NewIndex = self.SavingTasks[indexPath.row]
let view = self.storyboard?.instantiateViewController(withIdentifier: "TaskDetail") as! TaskDetail
view.SavingDetails = [NewIndex]
view.Index = indexPath.row
self.navigationController?.pushViewController(view, animated: true)
}
next class should be appeared
class TaskDetail: UIViewController {
var Delegate: NoteDetailDelegate!
var SavingDetails = [String]()
var Index: Int?
#IBOutlet weak var TaskDetailsFiled: UITextView!
#IBAction func SaveTDF(_ sender: UIButton) {
UserDefaults.standard.set(TaskDetailsFiled.text, forKey: "Saved")
self.navigationController?.popViewController(animated: true)
}
You can use a segue and prepare(for:sender:) to get the next view controller ready more easily than instantiating the view controller and popping it via code. Official documentation here and a sample app from Apple here
An implementation example:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mysegue"{
if let nextViewController = segue.destination as? NextViewController{
nextViewController.Index = 2
}
}
}
A highlight from the official doc:
For example, if the segue originated from a table view, the sender parameter would identify the table view cell that the user tapped
If you want to stick with the code implementation, you can call view.myvariable = myvalue in your didSelect

Recognize which tableviewcell is selected and pass value to another viewcontroller [duplicate]

This question already has answers here:
Send data from TableView to DetailView Swift
(4 answers)
Closed 5 years ago.
I'm trying to recognize which tableviewcell is selected and after that i want to get the label value of that cell and pass it to the next view controller. My cell has a Label value which is string and a Number value which is Int. I'm using a firebase database to get all that data.
My code:
import UIKit
class PlacesTableViewController: UITableViewController {
//MARK: Properties
#IBOutlet weak var placesTableView: UITableView!
var places = [Places]()
override func viewDidLoad()
{
super.viewDidLoad()
// Loads data to cell.
loadData()
}
override func numberOfSections(in tableView: UITableView) -> Int
{
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
//return the number of rows
return places.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "PlacesTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PlacesTableViewCell else {
fatalError("The dequeued cell is not an instance of PlacesTableView Cell.")
}
let place = places[indexPath.row]
cell.placeLabel.text = place.name
cell.ratingControl.rating = place.rating
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
print(places[indexPath.section])
self.performSegue(withIdentifier: "ShowCommentsTableViewController", sender: nil)
}
}
you can follow this thread to be useful send-data-from-tableview-to-detailview-swift
You have to create variable in destination view controller and store data in those Variable before Moving to Destination View controller.
This link will help you for this.
Try this:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let selectedPlace = places[indexPath.section]
self.performSegue(withIdentifier: "ShowCommentsTableViewController", sender: selectedPlace)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let selectedPlace = sender as? Places,
let destViewController = segue.destination as? SecondViewController {
destViewController.place = selectedPlace
}
}

Is there something like prepareForSegue when going back with UINavigationController?

I have an array in my ViewOne, which I pass to ViewTwo with the prepareForSegue function.
When I delete data of my array in ViewTwo and go back with the back button on the NavigationController or by swiping to right, the array in ViewOne has still all the data and don't know that I deleted something of it.
Is there any solution for this problem?
ViewOne:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "submitSegue") {
let shoppingCart = segue.destinationViewController as! ShoppinCartScreen;
if(artikel != nil) {
shoppingCart.alleArtikel = alleArtikel
print("Test: \(artikel?.artikelnummer)")
}
}
}
ViewTwo:
var alleArtikel = [Artikel]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return alleArtikel.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ShoppingCartScreenCell", forIndexPath: indexPath) as!ShoppingCartScreenCell
let eintrag = alleArtikel[indexPath.row]
cell.previewImage.image = eintrag.foto
cell.artikelNummer.text = eintrag.artikelnummer
return cell
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if(editingStyle == .Delete) {
alleArtikel.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
I have a main View (ViewOne) and a TableView(ViewTwo).
In Swift, arrays are value types not reference types (see Apple Developer Blog entry), so when you 'pass' it to view2 you are really just making a copy. View2 modifies the copy but the original remains unchanged.
Instead of using a prepareForSegue method when going back, which to my knowledge is not possible, just run the viewWillDisappear function. Basically this is the viewDidLoad function for when the view disappears. It will run when you are performing a segue when you are leaving the view.
example:
override func viewWillDisappear(animated: Bool)
{
super.viewWillDisappear(animated)
print("the view is hidden")
}

Resources