PrepareForSegue from a UIButton in a custom prototype cell
You could get the index path of the cell like so
let indexPath : NSIndexPath
if let button = sender as? UIButton {
let cell = button.superview?.superview as! UITableViewCell
indexPath = self.tableView.indexPathForCell(cell)
}
You'll also need to change your makeSegue
like this:
func makeSegue(button:UIButton) {
self.performSegueWithIdentifier("toCoriViewController", sender: button)
}
and in your cellForRowAtIndexPath
just change the line where you set the action to cell.button.addTarget(self, action: "makeSegue:", forControlEvents: UIControlEvents.TouchUpInside)
.
Alternatively, you could create a squadra
property inside your custom cell class to hold the arrayCori
value of that cell, so you'd have some code that looks like this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
let squadra = DataManager.sharedInstance.arrayCori[indexPath.row]
cell.squadra = squadra #add this line
cell.backgroundColor = UIColor.clearColor()
if (indexPath.row % 2 == 0)
{
cell.backgroundColor = UIColor.greenColor()
}
else
{
cell.backgroundColor = UIColor.blueColor()
}
//Here I added target to the button in the cell, and below in the class I implemented the fun makeSegue()
cell.button.addTarget(self, action: "makeSegue", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "toCoriViewController"
{
let controller = segue.destinationViewController as! CoriViewController
if let button = sender as? UIButton {
let cell = button.superview?.superview as! CustomCell
controller.coriSquadra = cell.squadra
}
}
}
Create Segue from Custom Cell UIButton to a ViewController
Ctrl-drag from the tableViewController (not from the prototype cell) to your target viewController and assign an identifier name to the segue.
Now override didSelectRowAtIndexPath
to perform the segue with your identifier when tapping that cell:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
performSegueWithIdentifier("yourSegue", sender: nil)
}
Distinguish between the cells based on your indexPath and create as many segues as you need to your target viewControllers.
You can do similar for the button in your custom cell. Perform the segue on the button's action.
Trigger Segue from UIButton in TabeViewCell
Your on the right track. In your tableviewcontroller define
@IBAction func clicked(){
self.performSegueWithIdentifier("toSecondController", sender: button)
}
You can keep the
tableView.deselectRowAtIndexPath(indexPath, animated: true)
line in your didSelectRowAtIndexPath. Don't forget to connect your ibaction to the button in your storyboard as well.
Show new ViewController after tapping UIButton in a custom cell
You can trigger when you click a cell with the delegate:
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
// some code
}
To open a new viewcontroller, if you use a storyboard you can try this:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let newVC = storyboard.instantiateViewControllerWithIdentifier("myIdentifier") as youClassController
self.presentViewController(newVC, animated: false, completion: nil)
Hope that help
Button in UITableViewCell — identify cell when button is pressed in prepareForSegue
-(void)yourButtonPressed:(id)sender
{
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:tableView];
NSIndexPath *hitIndex = [tableView indexPathForRowAtPoint:hitPoint];
NSLog(@"%i",hitIndex.row);
}
This is the best way to get " the button's row "
Pass data from prototype cell using button inside table cell
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "part", for: indexPath) as! partsTableCell
let part = partList[0]
cell.a.text = String(part.a)
cell.b.text = part.b
cell.c.text = part.c
cell.d.text = part.d
cell.e.text = part.e
cell.f.text = part.f
self.tableView.rowHeight = 250
cell.layer.borderColor = UIColor.darkGray.cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 5
cell.clipsToBounds = true
// give tag to your button and add target
cell.myButton.tag = indexPath.row
cell.myButton.addTarget(self, action: #selector(myButtonAction), for: .touchUpInside)
return cell
}
@objc func myButtonAction(sender:UIButton) {
// in button action do your stuff like below
let part = partList[sender.tag]
let detailVC = UIStoryboard(name: "YourStoryBoard", bundle: nil).instantiateViewController(withIdentifier: "partDetail") as! PartsVCDetail
detailVC?.a = part.a!
detailVC?.b = part.b!
detailVC?.c = part.c!
detailVC?.d = part.d!
detailVC?.e = part.e!
detailVC?.f = part.f
self.navigationController?.pushViewController(detailVC, animated: true)
}
Pass data from a button in reused custom cell
My Custom Cell:
protocol CustomCellDelegate {
func segueWithCellData(cell:CustomTableViewCell)
}
class CustomTableViewCell : UITableViewCell {
var delegate = CustomCellDelegate?
@IBAction func buttonTapped() {
if let delegate = self.delegate {
delegate.segueWithCellData(self)
}
}
}
CustomCellDelegate Method:
func segueWithCellData(cell:CustomTableViewCell) {
//Get indexpath of selected cell here
let indexPath = self.tableView.indexPathForCell(cell)
self.performSegueWithIdentifier("passMyData", sender: self)
}
Hence, no need of tagging cell.
Since, you have indexPath of the selected cell, you can get data from this and pass this through sender
parameter of performSegueWithIdentifier
method.
For example,
func segueWithCellData(cell:CustomTableViewCell) {
//Get index-path of selected cell here
let selectedIndexPath = self.tableView.indexPathForCell(cell)
let post = posts[selectedIndexPath.row]
self.performSegueWithIdentifier("passMyData", sender: post)
}
and, get the data inside prepareForSegue
as follows:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == “passMyData” {
let destination = segue.destinationViewController as! UINavigationController
let targetVC = destination.topViewController as! nextVC
//Get passed data here
let passedPost = sender as! Post
targetVC.title = title
}
}
Related Topics
Abstract Class and Abstract Function in Swift
Cloudkit - What to Do When a User Adds, Modifies or Deletes an Object While Offline
How to Run Terminal Command in Swift from Any Directory
Sending Array Data from One View Controller to Another
What's a Placeholder? Why Do We Need to Define Placeholder Data Type for Generics
How to Set .Realm File on Realm
Trouble Passing Array Through Prepareforsegue
Adding a UIvisualeffectview to Button
Loading Many UIimages from Disk Blocks Main Thread
Uipickerviewdelegate Xcode 8 Swift 3
Cannot Assign Value of Type 'Menuview' to Type 'some View'
How to Use Enumerated in a List of Structs in Foreach Swiftui
Disable Bringing App Window to Front. After Closing Another Window
Cache That Can Purge Unused Objects by Demand
Didbegincontact Not Being Called Swift