Search Bar in Table View Swift - ios

im doing a simple project in Xcode 6 and i want to add search bar in tableviewcontroller but something is not working for me. Im doing it by this tutorial
http://www.raywenderlich.com/76519/add-table-view-search-swift
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredProgramy.count
} else {
return self.programy.count
}
}
here im getting error "fatal error: unexpectedly found nil while unwrapping an Optional value". idk why. whole code is here
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredProgramy.count
} else {
return self.programy.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
var program : Program
if tableView == self.searchDisplayController!.searchResultsTableView {
program = filteredProgramy[indexPath.row]
} else {
program = programy[indexPath.row]
}
func filterContentForSearchText(searchText: String) {
// Filter the array using the filter method
var scope = String()
self.filteredProgramy = self.programy.filter({( program: Program) -> Bool in
let categoryMatch = (scope == "All") || (program.category == scope)
let stringMatch = program.name.rangeOfString(searchText)
return categoryMatch && (stringMatch != nil)
})
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
self.filterContentForSearchText(searchString)
return true
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchScope searchOption: Int) -> Bool {
self.filterContentForSearchText(self.searchDisplayController!.searchBar.text)
return true
}
}

self.searchDisplayController is nil.
I just downloaded the tutorial's sample code (which you should do as well) and I see that the author has a "Search Display Controller" in their nib file. Check your nib file and be sure that the Candy Search controller is hooked up properly.
It's supposed to look like this:
To get to that image right click on the Search Display Controller object in the .xib file. Notice in my image that "Referencing Outlets" has a connection between searchDisplayController and CandySearch. That's what you are missing.
To create the connection ctrl drag from the CandySearch controller to the `Search Display Controller" when you let go of the mouse you will see:
Click searchDisplayController and you should be good to go.
Lastly, you should read up on how optionals work in Swift to help you understand issues like this in the future:
https://developer.apple.com/librarY/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_456

I had a similar issue and found the following to work. The 'cell' variable in your code is nil because while you have set the number of rows, the actual cell object has not yet been created (line cell = UITableView(.. below)
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell : UITableViewCell
var player : Player
if self.searchDisplayController!.active {
var searchCell: AnyObject? = self.tableView.dequeueReusableCellWithIdentifier("Cell")
if searchCell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
} else {
cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
}
player = self.filteredPlayers[indexPath.row]
} else {
cell = self.tableView.dequeueReusableCellWithIdentifier(TableCellNamesEnum.PLAYER_DETAIL_CELL.rawValue, forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
player = self.selectedPlayers[indexPath.row]
}
cell.textLabel!.text = "\(player.firstName) \(player.lastName)"
return cell
}

Related

Ambiguous reference to member '(_:numberOfRowsInSection:)'

I'm trying to GET gists from Github and pop them in a table view,
here's the entire code, Gist is a class defined elsewhere:
var gists = [Gist]()
override func viewDidAppear(animated: Bool) {
loadGists()
}
func loadGists() {
GithubAPIManager.sharedInstance.fetchPublicGists() { result in
guard result.error == nil else {
print("Error 1")
return
}
if let fetchedGists = result.value {
self.gists = fetchedGists
}
self.tableView.reloadData()
//Error here.
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return gists.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")!
let gist = gists[indexPath.row]
cell.textLabel?.text = gist.description
cell.detailTextLabel?.text = gist.ownerLogin
return cell
}
So, the problem is I didn't add an outlet of the table view to the View Controller.swift.
Just dragged the table view to the .swift file to create it.

search display controller update table view

I'm making an app, which destination iOS is 7.0. So I use search display controller. When i try to search, i make an api request, and it's results are coming later, than search display controller updates table view. So it's empty, though i have results of search. I've tried something like
self.searchDisplayController?.searchResultsTableView.reloadData()
straight after reaching data from request, but it's not working.
Here is my logic:
func filterContextForSearchText(searchText: String) {
BooksWorker.searchForBooks(searchText) { foundBooks in
self.foundBooks = foundBooks
if BooksWorker.books != nil {
self.filteredBooks = BooksWorker.books.filter { book in
return (book.name?.lowercaseString.containsString(searchText.lowercaseString))!
}
}
self.searchDisplayController?.searchResultsTableView.reloadData()
}
}
func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String?) -> Bool {
isSearch = true
filterContextForSearchText(searchString!)
return true
}
I update my tableView in such a way:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearch {
tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
return filteredBooks.count
} else {
if BooksWorker.books != nil {
tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
return (BooksWorker.books?.count)!
} else {
showEmptyTableView()
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
return 0
}
}
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if isSearch {
let cell = tableView.dequeueReusableCellWithIdentifier(AppData.CellIdentifiers.UndownloadedBookCell) as! UndownloadedBookCell
print("making cell")
cell.setBook(foundBooks[indexPath.row])
cell.delegate = self
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier(AppData.CellIdentifiers.BookCell) as! BookCell
cell.setBook(BooksWorker.books![indexPath.row])
return cell
}
}
Does anybody having an idea?
you should update tableView not searchDisplay self.tableView.reloadData()

Each Cell need to have a Section - Parse and Swift

I'm implementing a Feed on my App using Parse.com, basically I'm populating a UITableViewController and everything works fine, BUT, I really like the way Instagram does, seems like the Instagram have a UIView inside each cell that works like a header and that view follows the scroll till the end of cell, I tried to search about that and I'm not successful, after some research I've realized that this feature is equally a Section, so I decide to implement Sections in my querys, I've implemented the code below:
import UIKit
class FeedTableViewController: PFQueryTableViewController {
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadCollectionViewData()
}
func loadCollectionViewData() {
// Build a parse query object
let query = PFQuery(className:"Feed")
// Check to see if there is a search term
// Fetch data from the parse platform
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
// The find succeeded now rocess the found objects into the countries array
if error == nil {
print(objects!.count)
// reload our data into the collection view
} else {
// Log details of the failure
}
}
}
// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
// Configure the PFQueryTableView
self.parseClassName = "Feed"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return objects!.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section)"
}
//override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! FeedTableViewCell!
if cell == nil {
cell = FeedTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
cell.anuncerPhoto.layer.cornerRadius = cell.anuncerPhoto.frame.size.width / 2
cell.anuncerPhoto.clipsToBounds = true
// Extract values from the PFObject to display in the table cell
if let nameEnglish = object?["name"] as? String {
cell?.title?.text = nameEnglish
}
let thumbnail = object?["Photo"] as! PFFile
let initialThumbnail = UIImage(named: "loadingImage")
cell.photoImage.image = initialThumbnail
cell.photoImage.file = thumbnail
cell.photoImage.loadInBackground()
return cell
}
}
Basically I will need to have a section for each cell, Now I'm successfully have sections working for each cell, but the problem is that the querys is repeating on the first post.
In the backend I have 3 different posts, so, in the App the UItableview need to have 3 posts with different content, with the code above I'm successfully counting the number of posts to know how many section I'll need to have and I declare that I want one post per section, but the app shows 3 sections with the same first post.
Any ideas if I'm capture the correct concept of the Instagram feature and why I'm facing this problem in my querys?
Thanks.
Keep the original UITableViewDataSource method and retrieve the current object using the indexPath.section
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! FeedTableViewCell!
if cell == nil {
cell = FeedTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
cell.anuncerPhoto.layer.cornerRadius = cell.anuncerPhoto.frame.size.width / 2
cell.anuncerPhoto.clipsToBounds = true
let object = objects[indexPath.section]
// Extract values from the PFObject to display in the table cell
if let nameEnglish = object["name"] as? String {
cell?.title?.text = nameEnglish
}
let thumbnail = object["Photo"] as! PFFile
let initialThumbnail = UIImage(named: "loadingImage")
cell.photoImage.image = initialThumbnail
cell.photoImage.file = thumbnail
cell.photoImage.loadInBackground()
return cell
}

Return Checkmark after using UISearchBar - Swift

I've created a UISearchBar App using this tutorial. Everything works fine, the cells are configured correctly, and I am able to search by username.
Now I am trying to add a (checkmark ✔︎) to each cell, allowing me to (select ✔︎) multiple users in a list.
The functionality works fine, but when I search the list, (select ✔︎) a user, and return to the main tableview, the user DOES NOT remain selected and vice-versa.
How can I (checkmark ✔︎) multiple users and maintain that checkmark before or after I use the UISearchBar?
class InviteViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate {
var allFriends = [Friend]()
var filteredFriends = [Friend]()
override func viewDidLoad() {
super.viewDidLoad()
###Call to get all Friends
getFriends()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredFriends.count
} else {
return self.allFriends.count
}
}
var selectedFriendIndex:Int? = nil
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
var friend : Friend
if tableView == self.searchDisplayController!.searchResultsTableView {
friend = filteredFriends[indexPath.row]
} else {
friend = allFriends[indexPath.row]
}
###Configure the cell
cell.textLabel!.text = friend.username
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
if (indexPath.row == selectedFriendIndex) {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
} else {
cell.accessoryType = UITableViewCellAccessoryType.None
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
selectedFriendIndex = indexPath.row
let cell = tableView.cellForRowAtIndexPath(indexPath)
if let index = selectedFriendIndex {
if (cell?.accessoryType == .Checkmark) {
cell!.accessoryType = .None
} else {
cell!.accessoryType = .Checkmark
}
}
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
self.filteredFriends = self.allFriends.filter({( friend : Friend) -> Bool in
var usernameMatch = (scope == "All") || (friend.username == scope)
var stringMatch = friend.username.lowercaseString.rangeOfString(searchText.lowercaseString)
return usernameMatch && (stringMatch != nil)
})
}
func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool {
self.filterContentForSearchText(searchString)
return true
}
func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchScope searchOption: Int) -> Bool {
self.filterContentForSearchText(self.searchDisplayController!.searchBar.text)
return true
}
func searchDisplayController(controller: UISearchDisplayController, willHideSearchResultsTableView tableView: UITableView) {
self.tableView.reloadData()
}
}
You need to keep track of what friends are selected in (an array in) your model.
You could add a Bool for each friend, indicating whether they are selected.
This will preserve the selection information when you search and return.
You will need to add a Bool variable to your object and set it to true or false.
contact.addToGroup = false
Swift 3 example:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
let contact = contacts[indexPath.row]
if (cell.accessoryType == .checkmark) {
cell.accessoryType = .none
contact.addToGroup = false
} else {
cell.accessoryType = .checkmark
contact.addToGroup = true
}
}
tableView.deselectRow(at: indexPath, animated: true)
}

Swift: Change button image in table view on click

I created an Xcode project for iPhone using Swift, with Parse for the backend. My current problem is with creating a todo list application as one tab of a larger application.
Inside of a custom prototype cell, I want to have a checkbox button that changes its image when clicked as well as changing the isChecked:Bool variable for that cell. I've gotten most of the way there, but I've run into a brick wall regarding setting and accessing the appropriate variables for this button.
Edit: Thanks to the answer below and other resources, I have finally written working code for this checkbox functionality. Essentially, the button's tag property is set equal to the indexPath.row of the PFObject. As my original question was a bit broad, I am updating my code below so that it might help other new developers who are working on similar functionality. There may be better ways, but this seems to work.
// TasksVC.swift
var taskObjects:NSMutableArray! = NSMutableArray()
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.fetchAllObjects()
}
func fetchAllObjects() {
var query:PFQuery = PFQuery(className: "Task")
query.whereKey("username", equalTo: PFUser.currentUser()!)
query.orderByAscending("dueDate")
query.addAscendingOrder("desc")
//fetches values within pointer object
query.includeKey("deal")
query.findObjectsInBackgroundWithBlock { (tasks: [AnyObject]!, error:NSError!) -> Void in
if (error == nil) {
var temp:NSArray = tasks! as NSArray
self.taskObjects = temp.mutableCopy() as NSMutableArray
self.tableView.reloadData()
println("Fetched objects from server")
} else {
println(error?.userInfo)
}
}
}
//MARK: - Tasks table view
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.taskObjects.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as TaskCell
var dateFormatter:NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "M/dd/yy"
var task:PFObject = self.taskObjects.objectAtIndex(indexPath.row) as PFObject
cell.desc_Lbl?.text = task["desc"] as? String
cell.date_Lbl.text = dateFormatter.stringFromDate(task["dueDate"] as NSDate)
//value of client within Deal pointer object
if let deal = task["deal"] as? PFObject {
// deal column has data
if let client = deal["client"] as? String {
// client has data
cell.client_Lbl?.text = client
}
}
//set the tag for the cell's UIButton equal to the indexPath of the cell
cell.checkbox_Btn.tag = indexPath.row
cell.checkbox_Btn.addTarget(self, action: "checkCheckbox:", forControlEvents: UIControlEvents.TouchUpInside)
cell.checkbox_Btn.selected = task["isCompleted"] as Bool
if (task["isCompleted"] != nil) {
cell.checkbox_Btn.setBackgroundImage(UIImage(named:(cell.checkbox_Btn.selected ? "CheckedCheckbox" : "UncheckedCheckbox")), forState:UIControlState.Normal)
}
cell.selectionStyle = .None
return cell
}
func checkCheckbox(sender:UIButton!) {
var senderBtn:UIButton = sender as UIButton
println("current row: \(senderBtn.tag)")
//retrieve the PFObject for the row we have selected
var task:PFObject = self.taskObjects.objectAtIndex(senderBtn.tag) as PFObject
println("objectID: \(task.objectId)")
if task["isCompleted"] as NSObject == true {
task["isCompleted"] = false
} else {
task["isCompleted"] = true
}
task.saveInBackgroundWithBlock { (success, error) -> Void in
if (error == nil) {
println("saved checkbox object in background")
} else {
println(error!.userInfo)
}
}
self.tableView.reloadData()
}
override func tableView(tableView: UITableView?, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView?, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
var task:PFObject = self.taskObjects.objectAtIndex(indexPath.row) as PFObject
task.deleteInBackgroundWithBlock({ (success, error) -> Void in
self.fetchAllObjects()
self.taskObjects.removeObjectAtIndex(indexPath.row)
})
} else if editingStyle == .Insert {
}
}
When working with tables and collection views, all the objects you have in a custom cell can be easily accessed in cellForRowAtIndexPath (for UITables)
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("ActionCell", forIndexPath: indexPath) as ActionCell
var action = actions[indexPath.row] as Action
cell.nameLabel?.text = action.name
cell.listLabel?.text = action.list
cell.dateLabel?.text = action.date
cell.checkboxButton = action.isChecked
cell.checkBoxButton.setImage(UIImage(named:"checkedImage"), forState:UIControlState.Normal)
return cell
}
more over I would suggest to change constants to variables. I'm new to Swift too and "let" declares a static variable.
I find very cool the use of the conditional operator (?:) in these cases:
cell.checkBoxButton.setImage(UIImage(named:(any_boolean_condition ? "checkedImage" : "uncheckedImage")), forState:UIControlState.Normal)
so it can return one image name for the condition True and another name for the condition False.

Resources