Open specific viewController in tableView - ios

I'm using a tableView like the iPhone's note application.
I can add/delete note (cells).
I'm facing a little issue when I delete a note.
Here's my code:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true)
switch (indexPath as NSIndexPath).row {
case 0:
let Liste1 = self.storyboard!.instantiateViewController(withIdentifier: "liste1") as UIViewController
self.present(Liste1, animated: false, completion: nil)
case 1:
let Liste2 = self.storyboard!.instantiateViewController(withIdentifier: "liste2") as UIViewController
self.present(Liste2, animated: false, completion: nil)
case 2:
let Liste3 = self.storyboard!.instantiateViewController(withIdentifier: "liste3") as UIViewController
self.present(Liste3, animated: false, completion: nil)
default:
break
}
As you can see, each indexPath.row can open a counted ViewController.
If I have 3 notes and I delete the 2nd, the 3rd will open the 2nd viewController. Tell me if you don't understand..
I know the problem , I don't know if there are some solutions to open specific viewController .
I want Cells is linked to a viewController, not by the indexPath..
Thanks in advance !

The implementation here is wrong. How it should be is that a ViewController (say named DetailsViewController) should be pushed whenever the user selects a cell. Only the text associated text/note should change. The correct note should be fetched from an array of all the notes (with index being the indexPath.row of the selected cell) and then pass is to DetailsViewController while presenting it.
Deleting a note:
When the user deletes a note, just delete the not from this array of all notes.

Related

How can I segue based on which cell was tapped in UITableViewCell?

I have a view controller with a table view embedded inside it, although it is always the same ten cells, I made it dynamic as you cannot make static table view cell inside a UIViewController. My question is, in prepare(for:) how can I segue to a view controller based on which cell was tapped? Each cell should navigatie to a completely different VC, but I think having ten segues is a mess.
Is there a better way?
You can implement the UITableViewDelegate protocol to actually detect the cell being tapped:
extension MyOriginViewController {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// I'm assuming you have only 1 section
switch indexPath.row {
case 0:
// Instantiate the VC
let vc = storyboard?.instantiateViewController(withIdentifier: "MyViewControllerIdentifier")
// Present the view controller, I'm using a UINavigationController for that
navigationController?.push(vc, animated: true)
case 1:
// Another VC and push or present modally
let vc = ...
self.present(vc, animated: true, completion: nil)
default:
// Default case (unconsidered index)
print("Do something else")
}
}
}
So if you have this:
You can do:
let vc = storyboard?.instantiateViewController(withIdentifier: "MyNavController")
self.navigationController?.present(vc, animated: true, completion: nil)

Open TableView To Last Clicked Cell

I have a tableview list with clickable cells, when one is clicked a new viewcontroller opens up. When a back button is clicked and the first VC is called, the tableview resets to the top of the list. How can I change this so when the back button is clicked the tableview goes back to the original cell clicked? From what I understand, I need a tableview.scrollToRow, but I'm getting a little lost in the indexPath that I need to select (believe I need to save the last selected row, but now sure how to do this)
Here's the code:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let webVC = UIStoryboard.init(name: "MainVC", bundle: nil).instantiateViewController(withIdentifier: "SecondaryVC") as! WebViewController
webVC.urlLink = self.listings?[indexPath.row].url
self.present(webVC, animated: true, completion: nil) }
override func viewWillAppear(_ animated: Bool) {
tableview.scrollToRow(at: indexPathSelected, at: .middle, animated: false)
}
Your problem is that you are actually moving forward to a new instance of your view controller rather than back to the existing instance. If you move back then the state of the view controller will be as you left it and there will be no need to do anything to the tableview.
You should use an unwind segue to move back
As your cell is respond to your touch(and you did not call deselectRow(at indexPath: IndexPath) after selecting cell), the tableView will keep your selection(s), so when you back to your viewController just call tableView.indexPathForSelectedRow(or tableView.indexPathsForSelectedRows) in your viewWillAppear() method.

How to assign Two segues on a table cell in SWIFT?

I am totally new at IOS swift and working with android.
Here is my issue :
I'm developing a simple chatting App of IOS. I've already completed android ver and this is a kind of clone.
It has view controllers named VCList, VCMyFriends and VCChat. VCList is my very first view controller and has a table view. Touching a table view cell changes current view controller to VCMyFriends or VCChat.
When I touch a cell, app checks the member counts of the cell, so if it has other members then go to VCChat. If not, VCMyFriends on to invite my friends. Like following pic.
What I found is, I cannot assign two segues on a cell or split a segue with two ways. So I thought that can I change my VC without a segue? However I could not find any references or tutorial about it.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if( memberCnt == 0 ) {
self.presentViewController(VCMyFriends(), animated: true, completion: nil);
} else {
self.presentViewController(VCChat(), animated: true, completion: nil);
}
}
Above is my last try, and failed. It moves somewhere, but shows nothing at all. And if I can, I wish to use segues because have some datas to pass with segue.
Take care.
What I normally do is to create an instance of the controller via the instantiateViewControllerWithIdentifier and set the necessary member variables before calling presentViewController. You can also add a NavigationController if you want the capability the go back to the previous window.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let mainStoryBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if( memberCnt == 0 )
{
let vcMyFriends = mainStoryBoard.instantiateViewControllerWithIdentifier("VCMyFriend") as! VCMyFriends
vcMyFriends.membervar1 = membervar1
vcMyFriends.membervar2 = membervar2
self.presentViewController(vcMyFriends, animated: true, completion: nil)
}
else
{
let vcChat = mainStoryBoard.instantiateViewControllerWithIdentifier("VCChat") as! VCChat
vcChat.membervar1 = membervar1
vcChat.membervar2 = membervar2
self.presentViewController(vcChat, animated: true, completion: nil)
}
}
membervar and membervar2 are the data that you pass to the View Controllers. You need to set it before calling presentViewController so that they will be available when viewDidLoad is called.
Simply do this
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if( memberCnt == 0 ) {
performSegueWithIdentifier("VCMyFriends_segue", sender: self)
} else {
performSegueWithIdentifier("VCChat_segue", sender: self)
}
}
In your storyboard, select the first segue (the red line in your image) and choose Attributes inspector, then add VCMyFriends_segue as identifier.
Do the same thing for the second line (blue) and add VCChat_segue identifier.
Hope this helps

Bar Button Item in TableViewCell is not registering in didSelectRowAtIndexPath

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedRow = indexPath.row
articleUser = userids2[indexPath.row]
self.performSegueWithIdentifier("jump", sender: self)
}
#IBAction func Share(sender: AnyObject) {
shareArticleUrl = articleUrls[selectedRow]
shareText = "Check Out This \(shareArticleUrl)"
let activityVC:UIActivityViewController = UIActivityViewController(activityItems: [shareText], applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
}
I currently have a toolbar in each cell of a tableview. After the update to Swift 2, when a bar button item is selected, instead of running the didSelectRowAtIndexPath function first it instead jumps directly to the share function. I need to use the indexpath.row in the didSelect function to select the proper item in the articleUrl list. The technique above worked fine until the update to Swift 2.
I have tried to incorporate let selectedIndex = self.tableView.indexPathForCell(sender as! UITableViewCell) in the share function but I haven't been able to make that work.
Why don't you set the tag on your tab bar as row number while adding it in your cellForRowAtIndexPath: and then in your Share function just retrieve it from the passed in sender.

How to pass a value to another ViewController by pushing in NavigationStack?

This is my code.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0{
self.navigationController?.pushViewController(self.storyboard?.instantiateViewControllerWithIdentifier("History") as! History, animated: true)
TV.deselectRowAtIndexPath(indexPath, animated: false)
}
}
I push to another ViewController(named History) and I want to pass my value which is in this ViewController to History ViewController.
I know we can pass value by using segue and segue's destinationViewController and sourceViewController.
But there is no segue in this case.
So,I don't know how to assign a var's value to History's var.
Anybody can help me?
let history = self.storyboard?.instantiateViewControllerWithIdentifier("History") as! History
//Here U can Access the variable of History
history.yourVar=Assign Value
self.navigationController?.pushViewController(history, animated: true)
TV.deselectRowAtIndexPath(indexPath, animated: false)

Resources