Swift - Cannot pass user gesture from custom UICollectionViewCell to UITextField inside it - swift

I have a custom UICollectionViewCell (let's call it MyCustomCell) that has a custom UITextField (let's call it MyCustomField) inside it that users can add text to. However, it seems no matter what I do, the tap inputs don't get from the MyCustomCell to its sub-view MyCustomField. Previously, I had this same arrangement, except MyCustomCell is a UITableViewCell, and that worked for some reason.
Right now I had to do a workaround, where whenever a cell is tapped, the cell sends a becomeFirstResponder() message to the textfield. However, this doesn't let the user tap on the "clear text" button inside the MyCustomField, nor does it let the user move the cursor around in the text field:
// function inside the MyCustomCell, called by the view controller
// whenever it thinks the user tapped on a cell:
func cellTapped()
{
// this is an instance of MyCustomField:
input.becomeFirstResponder();
}
Is there any way to "pass" the actual gesture to the MyCustomField (from either the MyCustomCell, or from the the view controller that manages them both)?
Update: this is what the view hierarchy looks like for the MyCustomCell (sub-class of UICollectionViewCell):

This may be of help to others who have similar issue: I believe #MultiColourPixel is correct in the comment above that there is a hidden "view" standing between the MyCustomField and MyCustomCell. What I did was:
Backed up the .xib and .swift files for the code for MyCustomCell and MyCustomField.
Delete the .xib and .swift files.
From the XCode menu, re-create the .swift file -- and make sure you check the "create XIB" as well when you create the .swift file / class that is subclass of the UICollectionViewCell.
Copy paste the old xib / swift contents into the new xib / swift.
It should work, without needing to explicitly send becomeFirstResponder() to the MyCustomField.

Related

Why Xcode refuses to show the TableViewCell class for the prototype cell on storyboard?

I have a TableViewObject on my storyboard which has a corresponding UITableView class.
Within that TableViewObject I also have the 'cell' which manages the layout of a given prototype cell - the class associated with this is of type UITableViewCell.
The issue is that no matter where on the storyboard I click, when I show assistant editor it opens the UITableView class - not the one corresponding to the UITableViewCell.
I think it hasn't always been this way because I managed to drag/drop controls to make outlets in the past but now it refuses to comply - what can I try to fix this?
I've confirmed that the UITableViewCell class is the one shown in the Identity Inspector for the cell and also tried deleting the Derived Data folder that someone recommended - but this hasn't solved it.
To give some further detail, for other TableViewController/Cell combos I have the option by clicking 'automatic' of selecting one of two files, but for the problematic one there is only one file (see screenshots).[![enter image description here][2]][2]
Don't use assistant editors at all. They are a dead letter. If you want to display the code for the table view cell subclass at the same time as the storyboard, just Option-click on the code file for the table view cell subclass in the project navigator — or select the prototype cell, switch to the Identity inspector on the right, and Option-click the little arrow next to the class name.

I can't create an outlet for a label in my collection view cell

So everything is done the way you would assume. I drag and drop a collection view into a controller view. I connect the datasource and delegate. blah blah blah everything is done by the book.
Ok so i want to connect a label inside my collectionViewCell to my viewController class. I use control+drag to drop it into my code and type in its name.
As soon as i hit connect, i get a red octagonal error. it says: "Illegal Configuration: The hoursPerDayOutlet outlet from the monthCellViewController to the UILabel is invalid. Outlets cannot be connected to repeating content."
I need to connect this though because as each cell is created it will have a different label.
Please help me fix and use swift. All advice is appreciated. Thanks!
You can't connect items inside a UICollectionViewCell to a UIViewController, create a UICollectionViewCell subclass, make the class of the cell to the subclass and then you can connect the items inside the cell to the subclass (not the controller). I recommend you to look at more tutorials on UICollectionView.
To add to the other answer, after creating a UICollectionViewCell subclass and verifying that the class of that cell is subclassed, you have to make sure that your UIViewController that you were originally trying to connect the outlet to is a UICollectionViewDelegate subclass.
After that you should be able to successfully create an outlet for your label, etc.

Swift: Add Action to a Table View Cell when value entered

I have a Table View Cell and have added an action to it that I want to execute when a value is changed.
I have added the action method in the viewController
#IBAction func valueEntered(sender: NSTextFieldCell)
{
print("valueEntered")
}
I can edit the Table View Cell by double clicking on it, but my method is never called.
I know I have done this before but I must be missing a step.
You have to hook up the NSTableView delegate.
In your case, control-click and drag from Reseller Table up to File's Owner and choose delegate.

Edit and add button for UITableView in ViewController

I have a little problem, I want to add a function to two BarItems in my UINavigationBar, which is placed above the UITableView in a normal ViewController. I'm very new to programming so can you please give me the complete code that I need? The functions I want to have are:
An edit button so I can delete and change the data of a cell (title + subtitle)
An add button so I can add a new cell to my UITableView. Maybe also a function that points to another ViewController where I can type the data for Title and subtitle to save it afterwards.
Thanks!
For editing record:
You cannot get the cell that your planning to edit using button on UINavigationController, the buttons should be placed at the same cell on your UITableview to edit that specific cell. and in this way you need custom UITableviewcell.
For creating new record :
First link the button to your UIViewController class as Action.
You can append the array that your using to fill the UITableview and then call your UITableview like to refresh the data:
self.tableview.reloadData()
For presenting another UIViewController:
First drag new ViewController in your storyboard and link the button on the first ViewController into your new UIViewController and choose segue as "Show".
To save your data its a big topic you have either NSUserDefaults or CoreData so Google it.
Note : linking means by pressing ctrl-drag with mouse to your target.
Update :
Download your sample from here : https://yadi.sk/d/EJf610XWhkxDT

How to call a view on click of each UITableViewCell programmatically?

I have created a UITableViewcontroller and a UINavigationController in a TableController.m
with UITableviewCell set to say #"CellOne" #"CellTwo". Now I also created two other files
`ImageView1.m`
ImageView2.m
where if I click on CellOne I should be able to get the view placed on ImageView1.m, same applied to the ImageView2.m. How should I achieve this programmatically without using nib file?
override the touches event in ImageView1 and ImageView2.