Segue with TableView - ios

I have some cell's (image bellow) created by tab[] I need to segue correct value to another view (moreViewController) on this view I have 'nazwa' 'szerokosc' 'dlugosc' and I need to save to nazwa correct "place" from cell etc
have somebody any solution? I search it but I don't find :/
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
self.performSegue(withIdentifier: "more", sender: tableView)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destViewController:moreViewController = segue.destination as! moreViewController
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nazwaTab.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! cellTableViewCell
let nazwa = nazwaTab[indexPath.row]
let szerokosc = szerokoscTab[indexPath.row]
let dlugosc = dlugoscTab[indexPath.row]
cell.nazwaLabel.text = nazwa
cell.szerokoscLabel.text = szerokosc
cell.dlugoscLabel.text = dlugosc
return (cell)
}

Declare a variable nazwa in MoreViewController and assign a value to it before segueing to it.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let identifier = segue.identifier ?? ""
switch identifier {
case "more":
guard let vc = segue.destination as? MoreViewController else {
return
}
vc.nazwa = self.clickedNazwa
default:
break
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
self.clickedNazwa = nazwaTab[indexPath.row]
self.performSegue(withIdentifier: "more", sender: tableView)
}

You can save a selected row to global variable and use it after call: prepare(for segue: UIStoryboardSegue, sender: Any?)

Related

How to pass tableView label to name variable in another viewController?

I'm creating an app for a school project, and I have a tableView with a label inside which contains the name of the cell, and I am trying to pass the name of the label to the navTitle variable on my second viewController. I have tried multiple ways of achieving this but have had no luck.
I have tried printing the passed title but it prints the output 'nil'
Home view
var names = ["Accomodation", "Facilities", "Things To Do", "About"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "homeCell", for: indexPath) as? homeTableViewCell
cell?.label.text = names[indexPath.row]
return cell!
}
//Passing data
var nameChosen : String?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath, animated: true)
nameChosen = names[indexPath.row]
performSegue(withIdentifier: "accomodationSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as? accomodationViewController
destinationVC?.navTitle = nameChosen
}
Accomodation view
var navTitle: String?
override func viewDidLoad() {
super.viewDidLoad()
print(navTitle)
// Do any additional setup after loading the view.
}
The easiest way is to pass the name as sender parameter, the property nameChosen is not needed.
And it's good practice to check the identifier of the segue in prepare(for segue
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath, animated: true)
let name = names[indexPath.row]
performSegue(withIdentifier: "accomodationSegue", sender: name)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "accomodationSegue" else { return }
let destinationVC = segue.destination as! accomodationViewController
destinationVC.navTitle = sender as! String
}
If the code crashes you made a design mistake.
And please name classes always with starting uppercase letter (AccomodationViewController).

pass data from tableview to another tableview

I am a beginner in swift and I really want to fix this problem. What I am trying is to pass data from tableview to another but when I run the code, it only show me a blank tableview. Here is my code in source tableviewcontroller:
import UIKit
class C1TableViewController: UITableViewController {
var categorisePagePOneName = ["CLOTHING", "SHOES", "ACCESSORIES", "THEME"]
var selectedIndex = Int()
override func viewDidLoad() {
super.viewDidLoad()
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categorisePagePOneName.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdt = "Conecell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdt, for: indexPath) as! C1TableViewCell
cell.textLabel?.text = categorisePagePOneName[indexPath.row]
// Configure the cell...
return cell
}
func laodForNextPage(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath, segue: UIStoryboardSegue, sender: AnyObject?) {
performSegue(withIdentifier: "connect2", sender: self)
self.selectedIndex = indexPath.row
if segue.identifier == "connect2" {
let vc = segue.destination as! C2TableViewController
vc.firstRow = categorisePagePOneName[self.selectedIndex]
}
}
}
Then here is my code in another tableViewController:
import UIKit
class C2TableViewController: UITableViewController {
var firstRow: String!
var secondArray: [String] = []
var clothingCat = ["ALL CLOTHING", "THEME", "JACKET", "T-SHIRT", "SHIRT", "SWEATSHIRT", "HOODIES", "PANTS", "JEANS", "SHORTS", "KNITWEAR", "VESTS"]
var shoesCat = ["ALL SHOES", "THEME", "BOOTS", "SANDALS", "SNEAKERS"]
var asscessoriesCat = ["ALL ASSCESSORIES", "THEME", "HAT","BAGS", "JEWELLERY", "EYEWEAR", "WATCHES", "WALLETS", "SOCKS", "UNDERWEAR"]
var themeCat = ["VINTAGE", "KOREA","JAPAN", "SPORTY"]
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return secondArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "C2", for: indexPath) as! C2TableViewCell
// Configure the cell...
switch firstRow {
case "CLOTHING":
secondArray = clothingCat
case "SHOES":
secondArray = shoesCat
case "ASSCESSORIES":
secondArray = asscessoriesCat
case "THEME":
secondArray = themeCat
default:
break
}
cell.textLabel?.text = secondArray[indexPath.row]
return cell
}
}
I am not sure if I am using switch in this case correctly
Replace (the custom method laodForNextPage is useless anyway)
func laodForNextPage(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath, segue: UIStoryboardSegue, sender: AnyObject?) {
performSegue(withIdentifier: "connect2", sender: self)
self.selectedIndex = indexPath.row
if segue.identifier == "connect2" {
let vc = segue.destination as! C2TableViewController
vc.firstRow = categorisePagePOneName[self.selectedIndex]
}
}
with
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
performSegue(withIdentifier: "connect2", sender: indexPath)
}
and implement
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "connect2" {
let indexPath = sender as! IndexPath
let vc = segue.destination as! C2TableViewController
vc.firstRow = categorisePagePOneName[indexPath.row]
}
}
And you can delete selectedIndex
You need to add this function while the sender is the indexPath
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
}

how to get data when cell accessory is tapped in ios

i have tableview which has several cells, when user tap on the cell it does some other function and when cell accessory is tapped i want it to segue to another view controller
i am able to do so but the problem is i am not able to send the data at cell indexpath row, in order to send data i first have to touch the cell then tap on accessory to send the data here is the code for tableview
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
myTableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case "ShowDetail":
if let ip = myTableView.indexPathForSelectedRow{
if let svc = segue.destination as? DetailViewController{
svc.selectedObject = (myCounters?[ip.row])!
}
}
default:
print("error in segue")
}
}
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "detailvc") as? DetailViewController
vc?.selectedObject = (myCounters?[indexPath.row])!
navigationController?.present(vc!, animated: true, completion: nil)
}
thanks to #shahzaib qureshi
i removed the segue connection and use instantiate view controller method as detail view controller in cell accessory tapped method
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "detailvc") as? DetailViewController
vc?.selectedObject = (myCounters?[indexPath.row])!
navigationController?.present(vc!, animated: true, completion: nil)
print("ewwewew")
}
Just pass the index path in the sender parameter, pretty easy:
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
performSegue(withIdentifier: "ShowDetail", sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowDetail" {
let indexPath = sender as! IndexPath
let svc = segue.destination as! DetailViewController
svc.selectedObject = myCounters![indexPath.row]
}
}
Try this,

func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
print("Index : \(indexPath.row)")
var tempIndex: Int!
var tempIdentifier: String!
self.tempIndex = indexPath.row //You can store indexpath.row value in temp variable. And then then you can access it while performing any other functions.
self.tempIdentifier = "ShowDetail" //This upto your needs.
}
//UIStoryboardSegue to move over another controller accordingly.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case "ShowDetail":
if let ip = myTableView.indexPathForSelectedRow{
if let svc = segue.destination as? DetailViewController{
svc.selectedObject = (myCounters?[tempIndex])!
}
}
default:
print("error in segue")
}
}

Cant trigger segue when selecting a table view cell (at once)

I am making a todo list app and have two View Controllers and I have to pass the Index Path of the selected row from the CategoryViewController to ItemsViewController and trigger the Segue...
This Is my Category View Controller
But when I Click one of the cell, it is just being selected but the segue is not triggered
Heres How
But the segue is triggered when I select another Cell after selecting First one...Here's My code
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToItems", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("sucess")
let destinationVC = segue.destination as! ToDoListViewControlorer
if let indexPath = tableView.indexPathForSelectedRow {
destinationVC.selectedCategory = categoriesArray[indexPath.row]
}
}
Send your indexPath.row in sender argument of performSegue
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "youridentifier", sender: indexPath.row)
}
get indexPath.row from the sender argument and pass to ToDoListViewControlorer
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("sucess")
let destinationVC = segue.destination as! ToDoListViewControlorer
if let indexPathRow = sender as? Int {
destinationVC.selectedCategory = categoriesArray[indexPathRow]
}
}
First, override didSelectRowAt method, because you want to handle moment when user select row, not deselect. Also as sender pass selected item
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToItems", sender: categoriesArray[indexPath.row])
}
and then in prepare(for:sender:) just downcast sender
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destination as? ToDoListViewControlorer {
destinationVC.selectedCategory = sender as! Category
}
}
p.s. name your controller Controller, not Controlorer

Swift Change navigation bar title to the title of the tableview cell that was selected

I'm trying to change the title of the new navigation bar to match the tableview cell that was pressed, moving the user to the next viewcontroller.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
folderCellSelected = folderNames[indexPath.row]
}
Grabs the name of the cell that was selected
override func viewDidLoad() {
super.viewDidLoad()
self.title = FoldersViewController.folderCellSelected
// Do any additional setup after loading the view.
mindmapNames.append("Task")
mindmapNames.append("Other Task")
}
And is supposed to insert the name of the cell, changing the title of the new page
But i can't figure out what to write instead of FoldersViewController.folderCellSelected
Instance member 'folderCellSelected' cannot be used on type 'FoldersViewController'
is the error i get
You can use the function override func prepare(for segue: UIStoryboardSegue, sender: Any?)
to send a value to the next view controller like that :
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var mytbl: UITableView!
var foldernames = ["test","test1"]
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "next"{
let nextViewController = segue.destination as! NextViewController
nextViewController.myTitle = foldernames[sender as! Int]
}
}
}
extension ViewController : UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return foldernames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = foldernames[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "next", sender: indexPath.row)
}
}
myTitle is a variable from NextViewController
and then in the NextViewController do this :
class NextViewController: UIViewController {
var myTitle = ""
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = myTitle
}

Resources