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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript