Swift UITextField inside TableViewCell - ios

I am using a table view and have a UITextField inside it, but this text field don't detect the click on the cell. How I can fix this?

subclass the uitextfield, in prepareforreuse, set the delegate to nil, in the cellforrowatindexpath in the uitableview/datasource delegate, set the the textfield delegate to self, then implement the tableview methods there. to determine which textfield is clicked, index at point functions for tableview in the UITableView header, once you find that point, convert it to NSIndexPath. use that indexpath to find the array value where you store the textfield text values, update that value, then reload the row or the entire tableview and viola, you have your textfield population and working correctly,
lastly, show your code, this is very difficult to answer without showing your code

Related

multiple segues in uitableview

I have a uitableview with 2 custom cells (messages, and notifications). I want to apply a segue to another viewcontroller when user press the message cell, but do nothing when he press the notification cell. how to do this with didselectrowatindexpath method? some answers here suggested using indexpath.row for checking which cell is clicked but I get these dynamic cells from json so I don't know where their position is in the tableview or how many are they.
using swift and xcode 7.3
If the cells are different types, you can just determine the class type in didselectrowatindexpath
Or you can add a different tag to the cells. You can do this in the storyboard or programatically when you create the cell.
Then in didselectrowatindexpath check what the tag is for the selected row and you will know what type of cell it is.

Updating label in a cell of UITableView

My UITableViewCell has a cell template which is created from another nib file. The cell has a UIlabel object. Now, once the UITableView has loaded and the text has been displayed, and if I want to change its value by clicking a button from another cell, How should I do it ?
I have updated the text of the UIlabel but how to show it on the screen? Should I reload the entire table? Kindly let me know if there is any good way to do it.
You can use KVO for this purpose. Each cell observes the model, and when it changes, update some fields.

UITextField values inside UICollectionView cell in swift

I have UITextField and an ImageView inside UICollectionView cell. It loads data from web service. The user can input data inside the text field and have to submit it. My problem is, when I scroll the collection view, the entered value in one text field in a cell got messed up with other text field values from another cell, and it displays wrong values.
my steps(suppose I have cells A, B, ......., k)
1.entering values
Cell A Textfield= 12
Cell B Textfield= 13
2.scrolling down
(I haven't entered anything in cell F Textfield, though it shows 13)
3.scrolling back to where I entered values
Cell A Textfield= 12
Cell B Textfield= (blank)
You can solve this problem by keeping the values of UITextField in an Array. Whenever you are entering value to UITextField and dismissing keyboard,then save that value to array at the same cell index value in array and when you scroll your collectionView, the textfield value should entered from array and it won't misplace value.
UICollectionView and UITableView reuse a handful of cell objects as you scroll. This helps with memory management. It also means that when you populate one text field on a cell, that text field's cell will get reused for a different row as you scroll, so if you don't reset the text field's content, you'll see the same data showing up for the wrong rows.
The solution is that your "source of truth" (aka model) must always be separate from your UI. So when the user types something in a text field, your model (perhaps an array of strings?) should be updated accordingly. Then when the user scrolls, you must use tableView:cellForRowAtIndexPath: or collectionView:cellForItemAtIndexPath: methods to populate the cell with the appropriate data. If there is no data, you must clear any data that may have been left over from another row whose cell got reused.
Other opportunities you'll have to deal with cell reuse include the willDisplayCellForRowAtIndexPath method on UITableViewDelegate, or prepareForReuse on UITableViewCell. Collection view objects have corresponding methods as well.
As far as I can understand from question I think the problem is, you are not updating values every time - cellForRowAtIndexPath function gets called on scrolling.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
what you need to do to resolve this issue is "You have to store all the values you have entered in your cell's textField and whenever above datasource method gets called for UITableView, updated cell accordingly".

How do i assign TextFields value (i.e text) to UITableViewCell?

What i want to do is that if i write something in TextField then it must get displayed in TableView
It is straight forward.
Implement delegates/datasource methods for UITableView.
Make an array to hold user input(s). This array will be used as datasource to populate the table.
Reload the tableView.
I always use searchBar in tableView, Implement searchBar's delegate ,then set cell's text changed when you input searchString.

UIPickerView in UITableViewCell - how to select values instead of scrolling TableView?

I've got a custom UITableViewCell that has a label and a UIPickerView. Display works fine, but when I want to select a value in the Picker, the TableView scrolls. What can I do so that the gestures in the cell go to the Picker instead of the whole TableView?
The only solution I could come up with was to set the whole TableView to scrollEnabled = NO. This works for the Picker, but now I can't get to the cells under the custom cell. Control has to be more fine-grained.
If you can get hold of the UIGestureRecognizer for each of the two gestures, and tell one it needs to wait for the other to fail. With one in every cell, that becomes over-wieldy.
Perhaps you should add a control or have the accessory view "bring up" the picker, and then dismiss it when done.

Resources