I implemented a UIRefreshControl on a TableView and additionally, I added a searchController with a searchBar.
The search as well as the pull to refresh work perfectly fine, the only thing I'm struggling with is how to deactivate the "pull to refresh" function when the searchController is active.
I tried implementing this solution, but somehow that did not work for me.
Make your viewcontroller conform the UISearchBarDelegate
class ViewController: UIViewController, UISearchBarDelegate
and implement two of its methods
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.removeRefreshControl()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
self.initRefreshControl()
}
In each method call the functions to set and remove the UIRefreshControl
func initRefreshControl() {
self.refreshControl = UIRefreshControl()
self.refreshControl?.addTarget(self, action: #selector(ViewController.refreshData), for: .valueChanged)
self.tableView.refreshControl = self.refreshControl
}
func removeRefreshControl() {
self.refreshControl = nil
}
The complete code is:
class ViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var searchBar: UISearchBar!
var refreshControl: UIRefreshControl?
override func viewDidLoad() {
super.viewDidLoad()
self.initRefreshControl()
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.removeRefreshControl()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
self.initRefreshControl()
}
func initRefreshControl() {
self.refreshControl = UIRefreshControl()
self.refreshControl?.addTarget(self, action: #selector(ViewController.refreshData), for: .valueChanged)
self.tableView.refreshControl = self.refreshControl
}
func removeRefreshControl() {
self.refreshControl = nil
}
#objc func refreshData() {
....
....
self.refreshControl?.endRefreshing()
}
}
This should work.
Related
I have a tableviewController and it uses a custom cell class for it's data.
The custom cell has a Mapview and a button.
I was able to connect the button to an event like this
class MyTVCellTableViewCell: UITableViewCell {
var placeButtonAction : (() -> ())?
var mapViewAction : (() -> ())?
#IBOutlet weak var mapView: MKMapView!
#IBOutlet weak var placeButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.deleteButton.addTarget(self, action: #selector(placeButtonSelected(_:)),for: .touchUpInside)
// This is where it fails as it does not recognize touchUpInside
self.mapView.addTarget(self, action: #selector(mapViewSelected()),for: .touchUpInside)
}
#IBAction func placeButtonSelected(_ sender: UIButton) {
placeButtonAction?()
}
func mapViewSelected(){
mapViewAction?()
}
Any events that I can insert in this line?
self.mapView.addTarget(self, action: #selector(mapViewSelected()),for: .touchUpInside)
If you want to see the tap position over the map as a geographic location, the following should do the job.
import UIKit
import MapKit
class MapViewController: UIViewController, MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(mapTapped))
tapGesture.numberOfTapsRequired = 1
mapView.addGestureRecognizer(tapGesture)
mapView.delegate = self
}
#objc func mapTapped(_ sender: UITapGestureRecognizer) {
let tapPoint = sender.location(in: mapView)
let geoPoint = mapView.convert(tapPoint, toCoordinateFrom: mapView)
print(geoPoint)
}
}
I want to do sth. each time the cancel button is tapped inside the searchBar.
It is not working and not even calling the searchBarCancelButtonClicked method.
https://i.stack.imgur.com/Iho1V.png
class TableViewController: UITableViewController, UISearchBarDelegate {
#IBOutlet weak var searchbar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
searchbar.delegate = self
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
print("Cancel Button Clicked")
}
}
I'm looking to implement a Pull-to-refresh, and I'm seeing that the pull-to-refresh action isn't smooth; it makes the Content View jump around the screen, like so:
https://imgur.com/a/6oWPizP
I have the following constraints:
My ViewController looks like this:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var textLabel: UILabel!
#IBOutlet weak var scrollView: UIScrollView!
var refreshControl: UIRefreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.addTarget(self, action: #selector(reloadData), for: UIControl.Event.valueChanged)
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
scrollView.refreshControl = refreshControl
}
#objc func reloadData() {
textLabel.text = "SURPRISE"
refreshControl.endRefreshing()
}
}
I want to implement UISearchController in UICollectionViewController. But i don't want to implement search in CollectionView I need SearchController in Swift.
I need to show Search Controller on Search Click
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { }
func searchBarResultsListButtonClicked(_ searchBar: UISearchBar) { }
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.view.addSubview(searchController.view)
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) { }
func updateSearchResultsForSearchController(searchController: UISearchController) {}
func updateSearchResults(for searchController: UISearchController) { }
I am adding when search is active but not showing
let searchController = UISearchController(searchResultsController: nil)
Please suggest me if any one have any idea .
I Swift, i make a Search View using UISearcheController and UISearcheResultUpdating
and i update the result with func updateSearchResultsForSearchController
if self.searchController?.searchBar.text.lengthOfBytesUsingEncoding(NSUTF32StringEncoding) > 0 {
let searchBarText = self.searchController!.searchBar.text
var arrResult = DFManager.GetResult(searchBarText)
self.results?.addObjectsFromArray(arrResult)
// Reload a table with results.
self.searchResultsController?.tableView.reloadData()
}
But that is result always updating when i type char by char, it make this app slowly. I want this result only update when type return key?
Try this:
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
//do something
searchBar.resignFirstResponder() //hide keyboard
}
And dont forget to add the searchBar delegate:
class SomeViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
searchBar.delegate = self
}
}