Button Segue in custom UITableViewCell - ios

I have a custom UITableViewCell with a button inside to segue the data to another viewController. This is by utilizing the
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//cell setup code
cell.button.tag = indexPath.row
cell.button.addTarget(self, action: "editRow", forControlEvents: .TouchUpInside)
return cell
}
then using the function
func editRow() {
self.performSegueWithIdentifier("editRow", sender: self)
}
the editRow segue is a storyboard segue.
The issue...Instead of performing the segue and passing the data, it fires the segue twice and doesn't transfer any data to the "SLEdit" viewController. Any ideas as to why this is happening? Please keep in mind I'm not very familiar with objective-c due to beginning learning with swift. If you need any further information or more code let me know. Thanks!
EDIT:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "editRow" {
let cell = sender as! SLTableViewCell
let indexPath = tableView.indexPathForCell(cell)
let SListController:SLEdit = segue.destinationViewController as! SLEdit
let items:SList = frc.objectAtIndexPath(indexPath!) as! SList
SListController.item = items
}
}

This is because you have already made a segue action in storyboard editor and you are calling the same segue again by assigning custom action to the button.
And to pass data to the next view controller use this method:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!){
if segue.identifier == "segueIdentifier" {
}
}
Edit 1:
The error: "Could not cast value of type UIButton" is due to this line:
let cell = sender as! SLTableViewCell
Here the sender UIButton and you are convertring/assigning(whichever term is suitable) to the TableViewCell.
Edit 3:
Replace your code by this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!{
if segue.identifier == "editRow" {
let button = sender as! UIButton
let view = button.superview!
let cell = view.superview as! <Your custom cell name here>
let indexPath = itemTable.indexPathForCell(cell)
let SListController:SLEdit = segue.destinationViewController as! SLEdit
let items:SList = frc.objectAtIndexPath(indexPath) as! SList
SListController.item = items
}
}

Related

Cannot perform segue from cell with indexPath

I am trying to perform a segue and pass data to detailViewController from my cell when tapped. I have tried with dummy data and I can pass them to detailScreen, however I would like to pass data that are related to cells.When I tapped a cell I get this error "Could not cast value of type ViewController to MovieCell" with line let selectedItem = ...
Thanks for your time and effort
Here is my code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detailSegue"
{
let destination = segue.destination as! DetailViewController
let selectedItem = collectionView.indexPath(for: sender as! MovieCell)!
let character = characters[selectedItem.item]
destination.detailName = character.name
destination.detailGender = character.gender
destination.detailSpecies = character.species
destination.detailStatus = character.status
}
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "detailSegue", sender: self)
}
try use:
collectionView.indexPathsForSelectedItems.first
instead
collectionView.indexPath(for: sender as! MovieCell)!
Also I wanna recommend for you my library for such situations: Link

UIImage not appearing

When I segue my UIImageView from my NewsTableViewController.swift, the image does not appear in my NewsDetailTableViewController.swift.
Here is an image of my simulator:
Here is an image of my Main.storyboard:
Here is my prepareForSegue() method code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let destinationController = segue.destinationViewController as! NewsDetailTableViewController
destinationController.item = items[indexPath.row]
let newsImage = UIImageView(image: UIImage(named: detailImages[indexPath.row]))
destinationController.image = newsImage
}
}
}
It is also worth mentioning that my image in my NewsTableViewController.swift is set to 0x0, and in my NewsDetailTableViewController.swift, it is set to 600x216. I don't know if this would matter or not. If you desire any code, please do feel free to ask.
FYI, I use Swift.
In prepareForSegue the IBOutlets of your destinationController have not been initialized yet. Just pass detailImages to another array property on NewsDetailTableViewController. Change your prepareForSegue to:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let destinationController = segue.destinationViewController as! NewsDetailTableViewController
destinationController.item = items[indexPath.row]
destinationController.imageNames = detailImages
}
}
}
Then in NewsTableViewController add the following code:
var imageNames:[String]?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//replace with whatever identifier you're using
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
cell.imageView?.image = self.imageNames[indexPath.row]?
return cell
}

Reciever has no segue with identifier swift 2

I am trying to pass value from Realm via segue to another viewcontroller. The problem is it is crashing with error : Reciever has no segue with identifier. I tried other way, prepareforsegue but it is not working as well as this one.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let lektire = datasource[indexPath.row]
let destinationVC = DetaljiViewController()
destinationVC.programVar = lektire.ime
destinationVC.performSegueWithIdentifier("oLektiri", sender: self)
}
However, my segue is definitely there, and its identifier is properly set (see screen shot below).
What is going on, and how can I fix this problem?
I believe this is what you are looking for you have to call performSegueWithIdentifier from the view controller you are currently inside of and either make the sender the indexPath or you can make it your datasource item.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("oLektiri", sender: indexPath)
// Another WAY
self.performSegueWithIdentifier("oLektiri", sender: datasource[indexPath.row])
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "oLektiri") {
let indexPath = sender as! NSIndexPath
let toViewController = segue.destinationViewController as? NextViewController
let lektire = datasource[indexPath.row]
toViewController?.selectedObject = lektire
}
// Another WAY
if(segue.identifier == "oLektiri") {
let lektire = sender as? lektireTYPE
let toViewController = segue.destinationViewController as? NextViewController
toViewController?.selectedObject = lektire
}
}
// An example of what the next view controller should look like
class NextViewController: UIViewController {
var selectedObject: ObjectType?
}

Buttons with push segues on superview of collectionviewcell which has storyboard segue throws error

I am new to swift and I am stuck with this error. I have a viewController which has a collection view, which has storyboard segue to a viewContoller and two buttons which have push segue to some other viewController. When I am trying to click on those buttons there is a fatal error, which says sender is not a collectionView with the below code.
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
if let cell = collectionViewOne.cellForItemAtIndexPath(indexPath)
{
performSegueWithIdentifier("showDeals", sender: cell)
}
else
{
// Error indexPath is not on screen: this should never happen.
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
assert(sender as? UICollectionViewCell != nil, "sender is not a collection view")
if let indexPath = self.collectionViewOne?.indexPathForCell(sender as! UICollectionViewCell) {
if segue.identifier == "showDeals" {
let detailVC: DealsTableViewController = segue.destinationViewController as! DealsTableViewController
detailVC.someCategory = MyIds[indexPath.row]
}
} else {
// Error sender is not a cell or cell is not in collectionView.
}
}
try to add
print("sender type : \(sender.dynamicType)")
inside your prepareForSegue , don't use assert, but optional chaining to cast the sender variable :
if let cell = sender as? UICollectionViewCell
instead of :
assert(sender as? UICollectionViewCell != nil, "sender is not a collection view")

Trouble passing a custom cell value to new viewController in swift

I have a tableview that uses custom cells. Problem is I do not know how to pass the value of a textField in my custom cell to the next view controller by using prepareForSegue. The code I am using is:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("StaffCell") as StaffCustomCell!
if cell == nil {
cell = StaffCustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: "StaffCell")
}
// Extract values from the PFObject to display in the table cell
cell?.staffNic?.text = object["Nic"] as String!
cell?.staffApellido?.text = object["Apellido"] as String!
var initialThumbnail = UIImage(named: "iboAzul")
cell.staffFoto.image = initialThumbnail
if let thumbnail = object["FotoStaff"] as? PFFile {
cell.staffFoto.file = thumbnail
cell.staffFoto.loadInBackground()
}
return cell
}
// Pass the custom cell value to the next view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueStaffSeleccionado" {
let detailViewController = segue.destinationViewController.visibleViewController as StaffDetailViewController
// This is the code I have no idea how to write. I need to get a value from the selected customCell
}
Any ideas? Thanks a lot
You get the selected cell via tableView.indexPathForSelectedRow. With that indexPath you have access to the cell:
// Pass the custom cell value to the next view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueStaffSeleccionado" {
let detailViewController = segue.destinationViewController.visibleViewController as StaffDetailViewController
if let indexPath = self.tableView.indexPathForSelectedRow() {
let cell = self.tableView.cellForRowAtIndexPath(indexPath)
// path the cell's content to your detailViewController
detailViewController.myProperty = cell.textLabel?.text
}
}
Another solution: If the segue is performed directly from the tableViewCell (by ctrl-dragging the segue from the cell in InterfaceBuilder) then the sender is the cell:
// Pass the custom cell value to the next view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
...
if let cell = sender as StaffCustomCell {
// path the cell's content to your detailViewController
detailViewController.myProperty = cell.textLabel?.text
}
}

Resources