I have a custom table view cell that's the first cell in my table view, in which I have a textfield I'm trying to make function like a search feature.
I want the data in the remainder of the table view to update as the user types. I have the search cell's textField set up via .addTarget to trigger EditingChanged so every time the user types something new into the textField it will show the updated results. But it seems that calling self.tableView.reloadData() triggers EditingDidEnd and hides the keyboard, after each letter.
Via addTarget to the Search Cell's UITextField, I have a function searchBarEditingChanged(searchTextField: UITextField) where after self.tableView.reloadData() I call searchTextField.becomeFirstResponder()
How can I get around this?
I was considering using a search controller earlier but I didn't like that you have to pull down to make the search bar visible
edit:
tried using this instead
let sectionsToReload = IndexSet(1...sectionHeaders.count)
self.tableView.reloadSections(sectionsToReload, with: .none)
but it crashes on reloadSections: 2017-05-22 15:37:21.600257 MYAPP[565:89501] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.6.22/UITableView.m:1284
Keep the textfield and the result in two different sections, on search reload only result section. This should work
Use the UISearchBar—this is what it is for.
view.endEditing(false) in the UITextFieldDelegate method.
Related
I am attempting to create a calculator that calculates results as UITextField values are changed. There is no submit button I figured textFieldDidEndEditing. Everything works with the exception of the UITextField where the user enters a value. The other fields on the form are UIPickerViews. It limits entry to 1 character in the free form UITextField.
self.mortgageAmount.delegate = self
func textFieldDidEndEditing(_ textField: UITextField) {
let selectedMortgageAmount = Double((mortgageAmount.text)!)
}
If you don't have a submit button and want dynamic calculation while typing into the field, you need to use textField(_:shouldChangeCharactersIn:replacementString:), not textFieldDidEndEditing(_:).
The former triggers each time any contents in the text field are about to change. The latter triggers only when the text field is done editing, either via a submit button or some other action, like scrolling the parent scroll view that is configured to end editing on scroll.
See https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619599-textfield
I want to edit the text in a cell by tapping and I was wondering if there is a way to do it other than by adding a text field. is there something in the table view delegate that I missed?
You can create cell (or any other UIResponder subclass) that conforms UITextInput protocol (or maybe UIKeyInput is enough). Here is simple example (old link)
Notice,
you should set cell.canBecomeFirstResponder = true.
To show keyboard use cell.becomeFirstResponder()
Maybe there something else, that you should do. I recommend reading docs.
...Or use UITextField :)
We use an array to display no of text label in cell.
so you can do is, you can use a text field and a button on a view.
In button action add a line of code like stringArray[2] = "new string" to replace the particular element from array and when you enter a text and press a button the element will get replaced and just reload the table view.
And you can also write the method in didSelectRowAtIndexPath, write stringArray[indexPath.row] = "new string" and reload the table view and it is done.
I have a strange problem: in my table view I want to update a single cell when its UISlider gets modified (value changed control event). At the moment the value of the slider changes, I want to animate a button into the cell (sliding in from the right). I set the constant of the button's constraint to -50 to make sure it's not visible by default, and when the slider's value gets changed, a method gets called which updates the table view, so cellForRowAtIndexPath gets called again for all cells in my table. Simplified it looks something like this:
func valueChanged(sender:UISlider) {
// Reload table view
self.myTableView.reloadData()
// Let table view know the value of the slider got modified
self.didChangeValueOfSlider = true
}
And in cellForRowAtIndexPath I'm keeping track of which cell's slider got changed by using a custom selectedCellIndexPath variable. When the table view gets to the cell that got modified: it runs the following code:
// Check if cell is selected
if indexPath == self.selectedCellIndexPath {
// Check if value of slider was changed
if self.didChangeValueOfSlider == true {
// Value was changed: set constraint back to default
saveButtonTrailingConstraint.constant = CGFloat(15)
self.view.setNeedsUpdateConstraints()
self.view.updateConstraintsIfNeeded()
// Reset slider update status
self.didChangeValueOfSlider = false
}
}
Calling those setNeedsUpdateConstraints() and updateConstraintsIfNeeded() might be overkill or unnecessary, but please note this is my 15th attempt or so to actually get the view to display the updated layout. I used breakpoints to confirm the constant actually changes, after the code above is finished running and everything works perfectly fine. The only thing I can't get working is the updating part. I've tried UIView.animateWithDuration to animate the change, and I've tried methods like layoutIfNeeded() and beginUpdates() and endUpdates of the table view: nothing works. What could be the reason the layout doesn't get updated? And what am I supposed to be calling the layout and update methods on? I've been calling them on self.view but I'm not even sure if you're supposed to be calling it on the view if you're trying to update the layout of a table view cell. Any help would be appreciated.
I figured it out. The problem turned out to be the constraint itself. as I was accessing the wrong one. I defined it as saveButtonTrailingConstraint but I was actually modifying was the saveButton's width constraint (which apparently can't be animated). Instead of accessing saveButton.constraints[indexOfTrailingConstraint] I should have defined saveButton.superview!.constraints[indexOfTrailingConstraint] as the constraint belongs to the superview and not to the button itself. I guess the best thing to take away from this clumsy mistake is to always double-check if you're modifying the correct constraint if you're doing it programmatically, because it doesn't always show on the first eye.
I am unable to find a solution for this on my own, please help.
I have a table view with -/+ buttons inside that increase or decrease a value.
In the footer cell of the table is a label for the "total sum", the added value of all the cell values. When the value inside of a cell is decreased or increased, I want the footer cell text to change accordingly.
Any pointers how I go about that? I have tried to write a global function, but then I can't access the label anymore... tried it as a class method (static), but I can't seem to get it to work.
Can I somehow hook up the action that occurs in the UITableViewCell to trigger an action in the UITableView?
Thank you!
In the UITableViewDelegate associated to your table view, you should call the reloadRowsAtIndexPaths(indexPaths: [NSIndexPath], withRowAnimation animation: UITableViewRowAnimation) method to update your the row you wish to update.
Than it your UITableViewDataSource provide the footer cell with the good new value.
I have several tableviews, one of them uses coredata and others get data from a json api. In my tableview which gets data from coredata, when I launch the app there is a cell, but my coredata is empty. Cell disappears when I reload the table.
How am I going to remove that cell?
You could simply add code to erase all cells if your core data is empty. You do this in the ViewWillAppear(), if your core data object is empty then return 0 at your numberOfRows in table.
You could also add a self update function (by NSNotifications) which reads the current amount of data (count) and then do a reloadData() call.
These are only suggestions... we don't really know how your code looks like to be able to suggest a better answer.
Cheers
You can simply add the following line of code in viewDidLoad() or viewWillAppear() to clear unwanted cells from UITableview.
tableView.tableFooterView = UIView(frame: CGRectZero)
If Table is initially empty then there will be no cell to display.