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;
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
add a comment |
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
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
add a comment |
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
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
ios swift xcode uitableview uisearchbar
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
Set your Delegates and DataSource:
override func viewDidLoad()
super.viewDidLoad()
dataTableView.delegate = self
dataTableView.dataSource = self
searchBar.delegate = self
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
InsidesearchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
you can checkif 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 throughsearchedUser
twice). Do you know how that might have happened?
– Richard Reis
Mar 24 at 8:22
Maybe clear thesearchedUser
beforesearchedUser = userData.filter
Let me know if that works
– Len_X
Mar 24 at 11:45
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Set your Delegates and DataSource:
override func viewDidLoad()
super.viewDidLoad()
dataTableView.delegate = self
dataTableView.dataSource = self
searchBar.delegate = self
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
InsidesearchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
you can checkif 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 throughsearchedUser
twice). Do you know how that might have happened?
– Richard Reis
Mar 24 at 8:22
Maybe clear thesearchedUser
beforesearchedUser = userData.filter
Let me know if that works
– Len_X
Mar 24 at 11:45
add a comment |
Set your Delegates and DataSource:
override func viewDidLoad()
super.viewDidLoad()
dataTableView.delegate = self
dataTableView.dataSource = self
searchBar.delegate = self
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
InsidesearchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
you can checkif 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 throughsearchedUser
twice). Do you know how that might have happened?
– Richard Reis
Mar 24 at 8:22
Maybe clear thesearchedUser
beforesearchedUser = userData.filter
Let me know if that works
– Len_X
Mar 24 at 11:45
add a comment |
Set your Delegates and DataSource:
override func viewDidLoad()
super.viewDidLoad()
dataTableView.delegate = self
dataTableView.dataSource = self
searchBar.delegate = self
Set your Delegates and DataSource:
override func viewDidLoad()
super.viewDidLoad()
dataTableView.delegate = self
dataTableView.dataSource = self
searchBar.delegate = self
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
InsidesearchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
you can checkif 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 throughsearchedUser
twice). Do you know how that might have happened?
– Richard Reis
Mar 24 at 8:22
Maybe clear thesearchedUser
beforesearchedUser = userData.filter
Let me know if that works
– Len_X
Mar 24 at 11:45
add a comment |
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
InsidesearchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
you can checkif 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 throughsearchedUser
twice). Do you know how that might have happened?
– Richard Reis
Mar 24 at 8:22
Maybe clear thesearchedUser
beforesearchedUser = 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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