Can I share same Cell of UICollectionView with UITableView? - xcode

I have created a cell.xib which is already subclass of UICollectionView already.
Later I decided to create a TableView in another ViewController... I wrote the code and everything except registering the cell I was unable to register the cell which is subclass of collectionView (which is registered already inside collectionView in other page) inside the TableView!
I got this warning or error
So, Please anyone can help me and tell me how to do it in the right way?

The problem is that FproductsCell is not a sublcass of UITableViewCell and thats why you get the error.
Create your UITableViewCell subclass in a xib and from this point, you have to register it to every UITableView where you want to use this cell in, programatically. Your class has to be inherited from UITableViewCell
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(YourCell.self, forCellReuseIdentifier: "YourIdentifier")
}

Related

Customized UITableViewCell didSelectRowAtIndexPath

I have a customized UITableViewCell that contains a few textviews and one imageview. These views are non-selectable (set in IB). I hope that didSelectRowAtIndexPath is triggered when users tap anywhere inside the cell. However, I find that it is not triggered when tapping on these views, even though these views are "in" the side. Looks like the "tapped" event is intercepted by these views. How can I make these views "no-tappable"?
Try this:
textView.userInteractionEnabled = false
Or uncheck "User Interaction Enabled" in IB:
#Bannings's answer seems corrected.
In other case, a workaround is using UITapGestureRecognizer (either on UITableView, UITableViewCell or UIView) to know if a cell was tapped, then call -selectRowAtIndexPath: on UITableView.

Trigger an action from a TableviewCell in swift

I'm new to Swift and I'd like know how to trigger an action when a cell in my TableviewController is tapped. I created the configuration of my tableview completely in the Storyboard, so the cells were not created manually by coding. I also edited the tag-field of this cells in order to address them, but I am wondering how.
Do I have to create a new class or is it possible to write the code in the basic viewcontroller.swift - class?
I just would like to know how such Events are handled in Swift and how I can catch them and react on them.
Thank you very much for answering.
Just add this code to your UITableViewController.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//In here do whatever you want to do when the user selects a row
}

How I can display another (change) ViewController when I press a button

in my xcode storyboard I have the standard viewController in which I have placed one Button with a Action Outlet.
I tried with an .xib which doesn't load. Further, I tried with an second ViewController in the Storyboard and a custom segue, but this also failed.
There is something I did wrong. Can anybody help me please?
If you are using Segues, you dont need to create a ViewController and try to display it this way. Just give a (unqiue) name to your ViewController (for Example if you dont want to use a "NavigationController" you should use "Modal" as Type. You should add a ViewController Class for each ViewController in Storyboard too.
So connect ViewController1 with your second View Controller and give them a name "loginModal".
Then in your IBAction, you can use:
self.performSegueWithIdentifier("loginModal", sender: self)
That should be all. If you want to send data between ViewControllers, you should use prepareForSegue. Check out my answer here, youll find more information about sending data with segues.
Sending data with Segue with Swift
You can try like this one
Put this code inside #IBAction button.
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let loginViewController = storyBoard.instantiateViewControllerWithIdentifier("LoginView") as loginViewController
self.presentViewController(loginViewController, animated:true, completion:nil)
Don't forget to set identifier's name in loginViewController.

Swift custom UITableviewCell width issue

I have created a custom UITableviewCell in xcode 6 beta(5), but while testing on the simulator it looks too small, I tried to set the width using attribute inspector, and it didn't worked. I created it using a .xib file and I registered it in my mainViewController to use it with connected tableView
This is how it looks like in simulator
The cell part of the code, where my custom class name is CustomCell
var cell:CustomCell = self.tableview.dequeueReusableCellWithIdentifier("cell") as CustomCell
cell.txt.text="\(ListArray.objectAtIndex(indexPath.row))"
return cell
This is how I register in viewDidLoad
var nipName=UINib(nibName: "CustomCell", bundle:nil)
self.tableview.registerClass(CustomCell.classForCoder(), forCellReuseIdentifier: "cell")
self.tableview.registerNib(nipName, forCellReuseIdentifier: "cell")
Take a look at my response to UILabels in custom UITableViewCell never gets initialized . First of all, code in the heightForRowAtIndexPath function. Second, depending on how you set up the nib/storyboard, if it already connected the tableview to the custom view, get rid of registerClass. Otherwise it'll disconnect your nib connection and recreate a new one, wiping out all the customization you did in the nib.

Switching from a UITableview controller to a viewcontroller

I'm just learning and playing with the apple Seismic XML example
http://developer.apple.com/library/ios/#samplecode/SeismicXML/Introduction/Intro.html
I've got most of it figured out, but the one area I can't get past is, if I want to remove the tableview controller and create a view controller populated with a tableview. I can get the tableview to appear fine, but no matter what I try I can never get it to populate.
In the viewdidload area I can setup the tableview, color the background, do whatever I want to do, but I seem to 'lose' control of it somehow.
In short, would someone please be able to give me the steps involved in correctly changing the tableviewcontroller to a viewcontroller with a tableview in the apple example?
Thank you.
Lian, you need to read the documentation on UITableViewController. http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html
I'm not sure what you mean by "remove the tableview controller and create a view controller populated with a tableview" or why you would want this configuration. If you're having trouble with populating the tableViewController, you just pass in the data, usually from an NSArray or NSDictionary in the cellForRowAtIndexPath method. You either need to select the UITableViewController template when you create the class file or include the UITableViewDataSource and UITableViewDelegate if you're adding a tableView to a ViewController.
If you mean you want to change the view controller on screen, then you're looking to pop to another view using the navigation controller.

Resources