How to transfer data from one tableViewCell to another tableViewCell in swift - swift

How i can transfer data from cell of tableView to cell of another tableView. I am confused about it. I can try delegate method,closure but they doesn't work on it. I want to show data of cell to cell of another tableView by single click on button. So please help me.
I try closure and delegate method to transfer data .but it doesn't work.Actually i am confused in tableView didSelectRowat function. It deal with cell on which i tapped and next step is when i tap on button this cell data shown on another cell which has different tableView & different viewController.

Assuming first tableviewcontroller is called "ItemsViewController"
and your second view controller is called "DetailViewController"
in your ItemsViewController add this to your didSelectRowAt delegate function:
then you can send that cells information to the next view according to it's IndexPath
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController {
vc.image = UIImage(named: images[indexPath.row])
vc.name = names[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}
}

Related

How to make 2ndVC tableview didSelectRowAt working only if i come from 1stVC btn tap in swift

I have two Viewcontrollers..
in 2ndVC i have tableview.. here i dont want didSelectRowAt method to work.. i need only this method to work when it navigates from 1stVC btnAction
in 1stVC textfield i need to fill with 2ndVC tableview's selected row text.. so i have placed button on textfield and in btnAction i gave navigation to 2ndVC.. now i need tableview selected row text in textfield
in 2ndVC i wrote didSelectRowAt here i dont want to select any row but in this viewcontroller if i select any row its going to 1stVC.. i want to select row only if i navigate from 1stVC btn
class 2ndVC: UIViewController {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "1stVC") as! 1stVC
viewController.empLoc = empDetail[indexPath.row]
self.navigationController?.pushViewController(viewController, animated: true)
}
}
1stVC code: if i go from here to 2ndVC then only i need to select tableview
class 1stVC: UIViewController {
#IBAction func empBtn(_ sender: Any) {
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "2ndVC") as! 2ndVC
self.navigationController?.pushViewController(viewController, animated: true)
}
}
how to stop tableview row selecting method in 2ndVC.. and select tableview row if come from 1stVC..
please help me with code.
Have a variable in 2nd viewController say isfromFirstVC and set to false by default. When you are presenting 2nd VC from 1st set that variable to true (viewcontroller.isfromFirstVC = true) . In 2nd VC make use of this variable in didSelectRowAt method.

I have a UItableView cell with a Image but can't get a image click to allow parameters

I have a EDIT2 UItableView, in Swift, with the normal labels and relevant Information. I have added a Image to the cell and all is working except when I click on the image I would like to open another ViewController with that particular cells relevant information. I have used:
let popup: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.popupalrmcontrnt))
cell.alrmwarning.addGestureRecognizer(popup);
as the means for calling the pushViewController function, But the Viewcontoller requires a parameter AlarmID to be set Beforehand. Said Viewcontroller needs AlarmID to fetch its relevant information.
Edit1 The parameter is dependant on the selected cell.
Create a closure variable in cell:
class SomeCell: UITableViewCell {
var imageClicked: (() -> Void)?
}
then in action of your tap gesture self.popupalrmcontrnt
add this line
func popupalrmcontrnt() {
self.imageClicked?()
}
then in your cellForRow, you can access this as:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
(cell as? SomeCell)?.imageClicked = {
//Navigate to your view controller here
}
return cell
}
Hope it helps!!
Better use UIButton. you can set image to the button as background image. Also you can pass relevant info as an object to the new ViewController.

didSelectCellAtIndexPath not called

I know there are a lot of relevant post on this and I have tried all of the ones I can find and unfortunately the problem still persists.
So I have a tableView inside of an UIViewController which is populated by 2 custom cells, one has got an UITextField and the other one got an UISwitch, and both have a UILabel. I then proceeded to implement the didSelectCell(at: IndexPath) method
I made sure the delegate is hooked up to the view controller.
Inside of the method, I simply wrote a print statement to ensure the taps are registering. However, the print message did to get printed to the console when I tapped on the UITextField and on the UILabel.
I have a feeling it is something to do with the firstResponder thing with the UITextField, and it is eating away my tap registration, but not really sure.
Any feedbacks are welcome!
UPDATE
Here is the setup on my custom cell:
Here is the didSelect method:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("tapped on cell")
if let cell = tableView.cellForRow(at: indexPath) as? NumericInputTableViewCell{
print("Tapped on numericcell")
} else if let cell = tableView.cellForRow(at: indexPath) as? BooleanInputTableViewCell {
print("Tapped on booleancell")
}
}
The delegate method is triggered when you click on the cell, not other elements on the cell. So create some empty cell in your table view and have another try. If it get printed out then that means you have elements covered over the cell and the cell itself is not clicked.
You can then disable user interaction for your UITextField stuffs from storyboard and then have a try. Ideally, when you click on the textfield on a table view cell, the correct respond I am thinking is that keyboard will show up and didSelectCell should not be triggered.
Here is the demo

How to add a subview to a UITableViewCell

I'm making a to-do planner using a table view. When the user presses enter after typing in a task, a button should appear to the left of the task.
The textFieldShouldReturn method in my ViewController class adds the button but not always in the top left corner of the screen. I have it set up like this:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as! TextInputTableViewCell
self.view.addSubview(cell.cellButton) // puts a button in a wrong location, button doesn't respond to any action
cell.addSubview(cell.cellButton)
tableViewData.append(textField.text!)
lastItem = tableViewData.count
print(tableViewData)
print(lastItem)
self.tableView.reloadData() //update row count and array count
textField.resignFirstResponder()
return true
}
self.view.addSubview(cell.cellButton)
adds a button to the view but the button doesn't appear in the right place, (i tried moving the location using storyboard, but it doesn't do anything). Plus the button doesn't respond to its appropriate click action, which is declared in the subclass.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell")! as! TextInputTableViewCell
cell.textField.delegate = self
...
return cell
}
Any help is appreciated. Let me know if I am not too clear
You're not grabbing the right cell in this line:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as! TextInputTableViewCell
You're basically telling the system to just give you a cell. You need to figure out what text field actually returned, and grabbing the cell that contains that text field and then adding your subview to the right cell. The way todo this would be to tag your textFields with the row number.
It works in the first example because you are in cellForRow, so you do have the right cell.
Would it be easier if you aligned the button in your .xib or storyboard file, and then kept it hidden until you needed it to appear?

pass data from tableview to view controller

I am writing a Xcode program in Swift. I have a tableview controller with some labels and an image per cell. They have their data from a first view controller. So far so good. Now i would like the user to tap a cell which opens a new controller which contains the same label and image data as the cell. With the following code the new controller opens, nonetheless I don't no how to transfer the data. If someone could help me I would be so so grateful.
Here are the codes that i tend to use:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "TableView"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TableView
let picture = pic[indexPath.row]
cell.label1.text = picture.name1
cell.photoImage.image = picture.photo
cell.label2.text = picture.name2
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let cell = indexPath.row
self.performSegueWithIdentifier("segue", sender: cell)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segue"{
var row = tableView.indexPathForSelectedRow
let viewController = segue.destinationViewController as! vc2
}
}
PS:I have a segue from the image to the new controller with the identifier "segue".
PPS: Of course i have tried the following method: Send data from TableView to DetailView Swift but when i run my program I get an error with the information that my labels were unexpectedly found nil
You should not need to override didSelectRowAtIndexPath or call performSegueWithIdentifier to do this. Connect your segue in the IB file dragging from a table view controller's cell to the second controller. You should then pass the data to the controller in prepareForSegue, in the segue.destinationViewController. You set the public properties on that destination controller.
Also make sure your labels in your prototype cell have been connected in IB. If they are they, should not be nil. Set a breakpoint on the cellForRowAtIndexPath to verify this.
Unless you are talking about the labels in your destination controller. These also need to be hooked up in IB. You would then set them in prepareForSegue. You would get the appropriate data from the
let viewController = segue.destinationViewController as! vc2
var path = tableView.indexPathForSelectedRow
viewController.destLabel.text = arrayData[path.row].myData // or whatever data you have from your model.