How to search tableview using data from API?How to Search Struct (Swift)How can I disable the UITableView selection?How to detect tableView cell touched or clicked in swiftExpand and Collapse tableview cellsUpdate or reload UITableView after completion of delete action on detail viewTableView not displaying text with JSON data from API callCreate a segue to another view controller through search bar?Swift remote Search crashes TableviewUnable to filter tableview created using array of dictionarySwift vertical UICollectionView inside UITableViewHow to search in Firebase Database Swift

Would the Geas spell work in a dead magic zone once you enter it?

Why do Russians call their women expensive ("дорогая")?

When and what was the first 3D acceleration device ever released?

Why are C64 games inconsistent with which joystick port they use?

Ticket sales for Queen at the Live Aid

How to plot an unstable attractor?

1960s sci-fi novella with a character who is treated as invisible by being ignored

Windows 10 Programs start without visual Interface

How do I align equations in three columns, justified right, center and left?

How to convert to standalone document a matrix table

How can I get exact maximal value of this expression?

Crossing US border with music files I'm legally allowed to possess

When did God say "let all the angels of God worship him" as stated in Hebrews 1:6?

Why colon to denote that a value belongs to a type?

Graph with same number of edges and vertices is a loop?

Smart people send dumb people to a new planet on a space craft that crashes into a body of water

Is this story about US tax office reasonable?

Is healing by fire possible?

When do characters level up?

Can a Beholder use rays in melee range?

How do you say “buy” in the sense of “believe”?

What is the object moving across the ceiling in this stock footage?

Plot twist where the antagonist wins

At what point in European history could a government build a printing press given a basic description?



How to search tableview using data from API?


How to Search Struct (Swift)How can I disable the UITableView selection?How to detect tableView cell touched or clicked in swiftExpand and Collapse tableview cellsUpdate or reload UITableView after completion of delete action on detail viewTableView not displaying text with JSON data from API callCreate a segue to another view controller through search bar?Swift remote Search crashes TableviewUnable to filter tableview created using array of dictionarySwift vertical UICollectionView inside UITableViewHow to search in Firebase Database Swift






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have built a Tableview that shows data called from an API (I mention this because maybe my problem is due to reloadData()).



However, the format of my data is simple (a struct) and I found an answer that addresses my question here.



But this didn't work for me. I'm not sure why. My code gets built and there are no errors, but when I search for something, nothing happens.



Here's what my code looks like:



import UIKit

class UsersViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var dataTableView: UITableView!

var userData: [User] = [
User(name: "name1", email: "email1", age: 25),
User(name: "name2", email: "email2", age: 25),
User(name: "name3", email: "email3", age: 25)
]

var searchedUser = [User]()
var searchActive = false

override func viewDidLoad()
super.viewDidLoad()




func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

let cell = dataTableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! UsersTableViewCell

if searchActive
cell.userName.text = self.searchedUser[indexPath.row].name // Maybe my error is that I'm just searching for their usernames, but that's what I want
else
cell.userName.text = self.userData[indexPath.row].name


return cell


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
if searchActive
return self.searchedUser.count
else
return self.userData.count






extension UsersViewController: UISearchBarDelegate
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
searchedUser = userData.filter(user) -> Bool in
return user.name.range(of: searchText, options: [ .caseInsensitive ]) != nil


searchActive = true

self.dataTableView.reloadData()





What have I done wrong?










share|improve this question

















  • 2





    Did you connect delegates? dataTableView.delegate = self; dataTableView.dataSource = self; searchBar.delegate = self;

    – Vasya2014
    Mar 24 at 7:33







  • 1





    Does the return function get called ? And do you set delegate and data source for TableView?

    – Len_X
    Mar 24 at 7:33












  • Thanks! The delegate and data source are what were missing. But now, after I search I get an empty table. How do I go back to my full table after emptying the search bar?

    – Richard Reis
    Mar 24 at 7:42












  • Set searchActive to false

    – Len_X
    Mar 24 at 8:08

















0















I have built a Tableview that shows data called from an API (I mention this because maybe my problem is due to reloadData()).



However, the format of my data is simple (a struct) and I found an answer that addresses my question here.



But this didn't work for me. I'm not sure why. My code gets built and there are no errors, but when I search for something, nothing happens.



Here's what my code looks like:



import UIKit

class UsersViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var dataTableView: UITableView!

var userData: [User] = [
User(name: "name1", email: "email1", age: 25),
User(name: "name2", email: "email2", age: 25),
User(name: "name3", email: "email3", age: 25)
]

var searchedUser = [User]()
var searchActive = false

override func viewDidLoad()
super.viewDidLoad()




func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

let cell = dataTableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! UsersTableViewCell

if searchActive
cell.userName.text = self.searchedUser[indexPath.row].name // Maybe my error is that I'm just searching for their usernames, but that's what I want
else
cell.userName.text = self.userData[indexPath.row].name


return cell


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
if searchActive
return self.searchedUser.count
else
return self.userData.count






extension UsersViewController: UISearchBarDelegate
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
searchedUser = userData.filter(user) -> Bool in
return user.name.range(of: searchText, options: [ .caseInsensitive ]) != nil


searchActive = true

self.dataTableView.reloadData()





What have I done wrong?










share|improve this question

















  • 2





    Did you connect delegates? dataTableView.delegate = self; dataTableView.dataSource = self; searchBar.delegate = self;

    – Vasya2014
    Mar 24 at 7:33







  • 1





    Does the return function get called ? And do you set delegate and data source for TableView?

    – Len_X
    Mar 24 at 7:33












  • Thanks! The delegate and data source are what were missing. But now, after I search I get an empty table. How do I go back to my full table after emptying the search bar?

    – Richard Reis
    Mar 24 at 7:42












  • Set searchActive to false

    – Len_X
    Mar 24 at 8:08













0












0








0








I have built a Tableview that shows data called from an API (I mention this because maybe my problem is due to reloadData()).



However, the format of my data is simple (a struct) and I found an answer that addresses my question here.



But this didn't work for me. I'm not sure why. My code gets built and there are no errors, but when I search for something, nothing happens.



Here's what my code looks like:



import UIKit

class UsersViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var dataTableView: UITableView!

var userData: [User] = [
User(name: "name1", email: "email1", age: 25),
User(name: "name2", email: "email2", age: 25),
User(name: "name3", email: "email3", age: 25)
]

var searchedUser = [User]()
var searchActive = false

override func viewDidLoad()
super.viewDidLoad()




func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

let cell = dataTableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! UsersTableViewCell

if searchActive
cell.userName.text = self.searchedUser[indexPath.row].name // Maybe my error is that I'm just searching for their usernames, but that's what I want
else
cell.userName.text = self.userData[indexPath.row].name


return cell


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
if searchActive
return self.searchedUser.count
else
return self.userData.count






extension UsersViewController: UISearchBarDelegate
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
searchedUser = userData.filter(user) -> Bool in
return user.name.range(of: searchText, options: [ .caseInsensitive ]) != nil


searchActive = true

self.dataTableView.reloadData()





What have I done wrong?










share|improve this question














I have built a Tableview that shows data called from an API (I mention this because maybe my problem is due to reloadData()).



However, the format of my data is simple (a struct) and I found an answer that addresses my question here.



But this didn't work for me. I'm not sure why. My code gets built and there are no errors, but when I search for something, nothing happens.



Here's what my code looks like:



import UIKit

class UsersViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var dataTableView: UITableView!

var userData: [User] = [
User(name: "name1", email: "email1", age: 25),
User(name: "name2", email: "email2", age: 25),
User(name: "name3", email: "email3", age: 25)
]

var searchedUser = [User]()
var searchActive = false

override func viewDidLoad()
super.viewDidLoad()




func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

let cell = dataTableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! UsersTableViewCell

if searchActive
cell.userName.text = self.searchedUser[indexPath.row].name // Maybe my error is that I'm just searching for their usernames, but that's what I want
else
cell.userName.text = self.userData[indexPath.row].name


return cell


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
if searchActive
return self.searchedUser.count
else
return self.userData.count






extension UsersViewController: UISearchBarDelegate
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
searchedUser = userData.filter(user) -> Bool in
return user.name.range(of: searchText, options: [ .caseInsensitive ]) != nil


searchActive = true

self.dataTableView.reloadData()





What have I done wrong?







ios swift xcode uitableview uisearchbar






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 24 at 7:14









Richard ReisRichard Reis

547




547







  • 2





    Did you connect delegates? dataTableView.delegate = self; dataTableView.dataSource = self; searchBar.delegate = self;

    – Vasya2014
    Mar 24 at 7:33







  • 1





    Does the return function get called ? And do you set delegate and data source for TableView?

    – Len_X
    Mar 24 at 7:33












  • Thanks! The delegate and data source are what were missing. But now, after I search I get an empty table. How do I go back to my full table after emptying the search bar?

    – Richard Reis
    Mar 24 at 7:42












  • Set searchActive to false

    – Len_X
    Mar 24 at 8:08












  • 2





    Did you connect delegates? dataTableView.delegate = self; dataTableView.dataSource = self; searchBar.delegate = self;

    – Vasya2014
    Mar 24 at 7:33







  • 1





    Does the return function get called ? And do you set delegate and data source for TableView?

    – Len_X
    Mar 24 at 7:33












  • Thanks! The delegate and data source are what were missing. But now, after I search I get an empty table. How do I go back to my full table after emptying the search bar?

    – Richard Reis
    Mar 24 at 7:42












  • Set searchActive to false

    – Len_X
    Mar 24 at 8:08







2




2





Did you connect delegates? dataTableView.delegate = self; dataTableView.dataSource = self; searchBar.delegate = self;

– Vasya2014
Mar 24 at 7:33






Did you connect delegates? dataTableView.delegate = self; dataTableView.dataSource = self; searchBar.delegate = self;

– Vasya2014
Mar 24 at 7:33





1




1





Does the return function get called ? And do you set delegate and data source for TableView?

– Len_X
Mar 24 at 7:33






Does the return function get called ? And do you set delegate and data source for TableView?

– Len_X
Mar 24 at 7:33














Thanks! The delegate and data source are what were missing. But now, after I search I get an empty table. How do I go back to my full table after emptying the search bar?

– Richard Reis
Mar 24 at 7:42






Thanks! The delegate and data source are what were missing. But now, after I search I get an empty table. How do I go back to my full table after emptying the search bar?

– Richard Reis
Mar 24 at 7:42














Set searchActive to false

– Len_X
Mar 24 at 8:08





Set searchActive to false

– Len_X
Mar 24 at 8:08












1 Answer
1






active

oldest

votes


















1














Set your Delegates and DataSource:



override func viewDidLoad() 
super.viewDidLoad()
dataTableView.delegate = self
dataTableView.dataSource = self
searchBar.delegate = self






share|improve this answer























  • This works! Thanks. But now after searching, my table is empty. And I tried setting searchActive to false in a pull to refresh method but that didn't work. Where should I do it?

    – Richard Reis
    Mar 24 at 8:13






  • 1





    Inside searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) you can check if searchText == "" searchActive = false self.dataTableView.reloadData()

    – Len_X
    Mar 24 at 8:16











  • Oh wait. My search results are showing up twice (almost like it's looping through searchedUser twice). Do you know how that might have happened?

    – Richard Reis
    Mar 24 at 8:22











  • Maybe clear the searchedUser before searchedUser = userData.filter Let me know if that works

    – Len_X
    Mar 24 at 11:45











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55321528%2fhow-to-search-tableview-using-data-from-api%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Set your Delegates and DataSource:



override func viewDidLoad() 
super.viewDidLoad()
dataTableView.delegate = self
dataTableView.dataSource = self
searchBar.delegate = self






share|improve this answer























  • This works! Thanks. But now after searching, my table is empty. And I tried setting searchActive to false in a pull to refresh method but that didn't work. Where should I do it?

    – Richard Reis
    Mar 24 at 8:13






  • 1





    Inside searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) you can check if searchText == "" searchActive = false self.dataTableView.reloadData()

    – Len_X
    Mar 24 at 8:16











  • Oh wait. My search results are showing up twice (almost like it's looping through searchedUser twice). Do you know how that might have happened?

    – Richard Reis
    Mar 24 at 8:22











  • Maybe clear the searchedUser before searchedUser = userData.filter Let me know if that works

    – Len_X
    Mar 24 at 11:45















1














Set your Delegates and DataSource:



override func viewDidLoad() 
super.viewDidLoad()
dataTableView.delegate = self
dataTableView.dataSource = self
searchBar.delegate = self






share|improve this answer























  • This works! Thanks. But now after searching, my table is empty. And I tried setting searchActive to false in a pull to refresh method but that didn't work. Where should I do it?

    – Richard Reis
    Mar 24 at 8:13






  • 1





    Inside searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) you can check if searchText == "" searchActive = false self.dataTableView.reloadData()

    – Len_X
    Mar 24 at 8:16











  • Oh wait. My search results are showing up twice (almost like it's looping through searchedUser twice). Do you know how that might have happened?

    – Richard Reis
    Mar 24 at 8:22











  • Maybe clear the searchedUser before searchedUser = userData.filter Let me know if that works

    – Len_X
    Mar 24 at 11:45













1












1








1







Set your Delegates and DataSource:



override func viewDidLoad() 
super.viewDidLoad()
dataTableView.delegate = self
dataTableView.dataSource = self
searchBar.delegate = self






share|improve this answer













Set your Delegates and DataSource:



override func viewDidLoad() 
super.viewDidLoad()
dataTableView.delegate = self
dataTableView.dataSource = self
searchBar.delegate = self







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 24 at 8:11









Len_XLen_X

317320




317320












  • This works! Thanks. But now after searching, my table is empty. And I tried setting searchActive to false in a pull to refresh method but that didn't work. Where should I do it?

    – Richard Reis
    Mar 24 at 8:13






  • 1





    Inside searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) you can check if searchText == "" searchActive = false self.dataTableView.reloadData()

    – Len_X
    Mar 24 at 8:16











  • Oh wait. My search results are showing up twice (almost like it's looping through searchedUser twice). Do you know how that might have happened?

    – Richard Reis
    Mar 24 at 8:22











  • Maybe clear the searchedUser before searchedUser = userData.filter Let me know if that works

    – Len_X
    Mar 24 at 11:45

















  • This works! Thanks. But now after searching, my table is empty. And I tried setting searchActive to false in a pull to refresh method but that didn't work. Where should I do it?

    – Richard Reis
    Mar 24 at 8:13






  • 1





    Inside searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) you can check if searchText == "" searchActive = false self.dataTableView.reloadData()

    – Len_X
    Mar 24 at 8:16











  • Oh wait. My search results are showing up twice (almost like it's looping through searchedUser twice). Do you know how that might have happened?

    – Richard Reis
    Mar 24 at 8:22











  • Maybe clear the searchedUser before searchedUser = userData.filter Let me know if that works

    – Len_X
    Mar 24 at 11:45
















This works! Thanks. But now after searching, my table is empty. And I tried setting searchActive to false in a pull to refresh method but that didn't work. Where should I do it?

– Richard Reis
Mar 24 at 8:13





This works! Thanks. But now after searching, my table is empty. And I tried setting searchActive to false in a pull to refresh method but that didn't work. Where should I do it?

– Richard Reis
Mar 24 at 8:13




1




1





Inside searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) you can check if searchText == "" searchActive = false self.dataTableView.reloadData()

– Len_X
Mar 24 at 8:16





Inside searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) you can check if searchText == "" searchActive = false self.dataTableView.reloadData()

– Len_X
Mar 24 at 8:16













Oh wait. My search results are showing up twice (almost like it's looping through searchedUser twice). Do you know how that might have happened?

– Richard Reis
Mar 24 at 8:22





Oh wait. My search results are showing up twice (almost like it's looping through searchedUser twice). Do you know how that might have happened?

– Richard Reis
Mar 24 at 8:22













Maybe clear the searchedUser before searchedUser = userData.filter Let me know if that works

– Len_X
Mar 24 at 11:45





Maybe clear the searchedUser before searchedUser = userData.filter Let me know if that works

– Len_X
Mar 24 at 11:45



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid …


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55321528%2fhow-to-search-tableview-using-data-from-api%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?