Trigger an action from a TableviewCell in swift - xcode

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
}

Related

Can I share same Cell of UICollectionView with UITableView?

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")
}

NSTableView Delegate Methods aren't getting called by .reloadData()

This one is stumping me, though I know it's going to be simple.
I have multiple ViewControllers, each of which has a table view to display a list of data specific to the controller. Three out of the 4 work perfectly. However, I'm clearly missing something in the forth, because the Delegate methods are never getting called by .reloadData(). No compiler errors, just never calls my delegate methods for some reason.
Here is my class setup including my connected outlet variable:
class performanceViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
//--------------------------------
// OUTLET CONNECTIONS
//--------------------------------
#IBOutlet weak var displayPerformanceList: NSTableView!
I've also implemented the following two delegate functions:
func numberOfRows(in tableView: NSTableView) -> Int {
and
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
but for some reason, neither of these functions are called when I execute my custom function:
func redrawViews() {
self.displayPerformanceList.reloadData()
}
(Note: the .reloadData is wrapped in a function because it will be doing other things as well that I haven't added in yet.)
I added breakpoints in both of the delegate functions, which is how I know they are not getting executed.
I know this is going to turn out to be something stupid that I missed since I have it working in three other custom viewControllers... I just can't for the life of me figure out what I missed...
Any thoughts would be greatly appreciated!
:)
Well, after poking around a bit more and following some tutorials, I found my problem. I completely forgot to link the NSTableView Data Source and Delegate to the controller in the Interface Builder!
Doh!
Leaving this question/answer here in case anyone else gets caught by this. :)

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.

How to show necessary methods to be implemented for specific protocol

I am absolute beginner with IOS and XCode and swift and I am doing my first tries with Xcode in a Yosemite built in a Vmware running under Windows7 (guest is MAC yosemite, Host is windows running vmware workstation 9 )
I have a ViewController Class in my IOS project declared as follow:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {...}
Now I would like to implement the necessary methods of the protocols UIViewController and UITableViewDelegate.
In a tutorial
http://jamesonquave.com/blog/developing-ios-apps-using-swift-tutorial/
it says I have to use Command+Click on a protocoll to show which methods should be implemented.
I don't know howto do "command+click on the protocoll" this with my vmware.
any hints ?
When using Alt+clicking on a protocoll it shows me the help. If I am WindowsButton+clicking on a protocoll it opens a new window with sourcecode of the protocoll. Now howto show methods, which should be implemented.
To help you out, UIViewController is a class, which you are subclassing. It is not a protocol, so it doesn't have any necessary methods for you to implement. You can however override the methods you inherit, which is what you see in when you first create the file, ex.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
This overrides the method you inherited from UIViewController. If you aren't doing anything other than calling super.viewDidLoad() you can remove the entire function from your code.
Now UITableViewDataSource and UITableViewDelegate or both protocols, so they may or may not have required methods to implement.
What you describe when you alt-click, is the same as if you right clicked on UITableViewDataSource and selected jump to definition. This is what you get when you command click on an actual Mac. What the tutorial is saying is that the required methods will be at the top, which isn't always the case if you look at the documentation (as the methods are organized more by purpose there). In the case of UITableViewDataSource, you should see something like:
protocol UITableViewDataSource : NSObjectProtocol {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
optional func numberOfSectionsInTableView(tableView: UITableView) -> Int // Default is 1 if not implemented
optional func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? // fixed font style. use custom view (UILabel) if you want something different
optional func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String?
You will notice that the first two methods func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int and func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell do not have optional in front of them. This means that these are the required methods for this protocol. The optional ones below that are optional.
In the case of UITableViewDelegate you'll see that there are no non-optional methods, and therefore it does not require you to implement any.
You are doing the right thing when you windows click the protocol name to jump to its definition, which is basically just a list of all its methods you can copy and paste into your code
Edit
the below is for changing keyboards:
You could try swaping the keys like this:
I don't like putting external source links because they sometimes dissapear so below is the text in their how to:
To swap the Option/Alt key and the Command/Windows key:
Fusion 2.x and later
Go to VMware Fusion > Preferences.
Click Keyboard & Mouse, and then Key Mappings.
Find the line that has a Mac Shortcut of Option,
and double-click it to edit it.
If you don't see this line, click the + button and then select
Option in the top row of keys.
In the To mapping in the bottom row of keys ensure that Alt is not
slected and that the Windows logo is selected.This will ensure that
pressing the Option key will send the Windows key to the virtual
machine.
Click OK.
Find the line that has a Mac Shortcut of the
command key logo, and double-click it to edit it.
If you don't see this line, click the + button and then select the
command key logo in the top row of keys.
In the To mapping in the bottom row of keys ensure that the Windows
logo is not selected and that Alt is selected.This will ensure that
pressing the Command key will send the Alt key to the virtual
machine. Click OK.
Fusion 1.x
Shut down your virtual machine and quit Fusion.
Navigate to [Macintosh HD]//Library/Preferences/VMware Fusion
Ctrl-click the config file and select Open with.
Select TextEdit and click Open. Add the line:
-mks.keyboard.swapAlt = TRUE
When your virtual machine starts, the Option/Alt key and the Command/Windows key are reversed.

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.

Resources